简体   繁体   中英

How to open new window from MVC Controller?

This is my code

 public ActionResult Contact()
    {
        //var getRequest = (HttpWebRequest)WebRequest.Create("https://brisol.net/av-d");
        //var getResponse = (HttpWebResponse)getRequest.GetResponse();

        //return new HttpWebResponseResult(getResponse);
        return ExternalGet("https://sample.net/av-d");

    }


    /// <summary>
    /// Makes a GET request to a URL and returns the relayed result.
    /// </summary>
    private HttpWebResponseResult ExternalGet(string url)
    {
        var getRequest = (HttpWebRequest)WebRequest.Create(url);
        var getResponse = (HttpWebResponse)getRequest.GetResponse();

        return new HttpWebResponseResult(getResponse);
    }

In the above code when i click contact link it open the below url with in the same window,but i want to open new window from mvc controller.

That is not an issue of the MVC. It depends on your HTML code. The link which should be opened in a new window should look like this:

<a href="some url" target="_blank">Click me</a>

Please, notice the target attribute. However, keep in mind that it is up to the browser whether it opens new window or just a new tab.

You can use target for blank page

HTML Code

<input type="button" target="_blank"/>

MVC Code

 @Html.ActionLink(" ", "ActionName", "ControllerName", null, new { target = "_blank", @class = "btn btn-primary fa fa-list"})

New tab/window can be controlled from your html page actually, not from server side code.

Use target='_blank' attribute in your anchor ( <a> ) tag.

If you are using plain HTML, do:
<a href="https://developer.mozilla.org" target="_blank">MDN</a>

For rajor helper, do
@Html.ActionLink("visit the page", "action", "controller", new {target="_blank"})

Here is the help page: https://developer.mozilla.org/en/docs/Web/HTML/Element/a

Things like that should be done from the view or from javascript not from the controller.

javascript:

window.open("Link URL")

razor

@Html.ActionLink("bla", "Action", new {controller="Controller"}, new {target="_blank"})

or

<a href="@Url.Action("Action", "Controller")" target="_blank">bla</a>
Dim strScript As String = "window.open('../Newpage.aspx', 'new_window');"

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "Open window", strScript, True)

Below Code will help you

Controller:-

    public ActionResult ABC()
    {
        if(Request.HttpMethod == "POST")
            return Redirect("http://www.google.com");
        else
            return View();
    }

View:-

@using (Html.BeginForm("ABC", "Test", FormMethod.Post, new { target = "_blank" }))
{
   <input type="submit" value="Contact" id="btnsubmit" />
}

Above example is just give you idea how to open new url from the controller you need to set this idea in your code.

In this example controller is Test , when you post the request like click on Contact then post the view with target = “_blank” so response of the controller action will be open in new window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM