Neill
Neill

Reputation: 711

In ASP.NET MVC, how can I open a static .html page in a new tab from an action method in a controller?

I am adding static help pages to my MVC application.

Clicking the help link directs to an Action Method with a unique identifier, telling the controller which page to show.

I know it can be done from within the View using Javascript or an anchor tag.

You can also open the static page from the controller, but it does not open in a new tab:

var result = new FilePathResult("~/Help/index.html", "text/html");
return result;

I cannot seem to open this static page in a new tab from the controller. Is this even possible?

EDIT

As to why How do you request static .html files under the ~/Views folder in ASP.NET MVC? does not solve my problem:

My static file do not live in the Views folder and it also does not address opening the page in a new tab.

EDIT 2 - Solution

Since this cannot be done directly from the controller I implemented the following in my View's scripts.

$('#linkAppHelpButton').off().on('click', function () {
        $.ajax({
            type: "GET",
            url: '@Url.Action("ReturnHelpPage", "Help", null)',
            data: { identifier: "index" },
            success: function (page) {
                window.open(page, '_blank');
            },
        })
    });

Upvotes: 0

Views: 2343

Answers (1)

NightOwl888
NightOwl888

Reputation: 56869

Since controllers are processed on the server side, it is not possible to control aspects of the browser such as "open in a new tab".

You can (as you have discovered) serve an HTML file through a controller action or just allow the web server to serve it directly. But the only options for opening the content in a new tab are to use <a target="_blank" href="some URL">new tab</a> or use JavaScript.

Upvotes: 1

Related Questions