How do you perform a 302 redirect using MVC3 and ASP.Net?
I'm using Razor, HTML5, MVC3 with C# in an application, where after a user clicks on a link, I open a new window, do some processing, then want to redirect that wi开发者_StackOverflow中文版ndow with a 302 status code to a link.
Thanks.
The correct way to do this in ASP.NET MVC is by having a controller action that returns a redirect ActionResult
. So inside the controller action you are invoking in this window and doing the processing simply perform a redirect by returning a proper ActionResult:
public ActionResult Foo()
{
// ... some processing
return RedirectToAction("SomeAction", "SomeController");
}
When the Foo
action is invoked (presumably inside the new window) it will do the processing and return a 302 HTTP status code to the client with a new location of /SomeController/SomeAction
.
If you wanted to redirect to some external url of your application you could do the following:
public ActionResult Foo()
{
// ... some processing
return Redirect("http://someotherdomain.com/somescript");
}
As far as creating a link that will open in a new window/tab is concerned you could append the target="_blank"
attribute on the anchor:
@Html.ActionLink(
"Some link", // linkText
"Foo", // action
"SomeController", // controller
null, // routeValues
new { target = "_blank" } // htmlAttributes
)
精彩评论