Add a simple route to the Routeconfig
//New Routeroutes. MapRoute (Name:"article", URL:"Detial/{id}", defaults:New{controller ="article", action ="detial", id =urlparameter.optional}, Constraints:New{id =@"\d+" } //namespaces:new string[] {});
302 redirects
PublicActionResult UrlTest1 () {//302 returnRedirect ("/ARTICLE/DETIAL/1"); } PublicActionResult UrlTest2 () {//302 returnRedirecttoaction ("detial","article",NewSystem.Web.Routing.RouteValueDictionary (New{id =2 })); //return redirecttoaction ("Detial", "article", new {id = 1}); } PublicActionResult UrlTest3 () {//302 returnRedirecttoroute ("article",NewSystem.Web.Routing.RouteValueDictionary (New{id =3 })); //return Redirecttoroute ("article", new {id = 1});}
301 Redirects
PublicActionResult UrlTest4 () {//301 returnRedirectpermanent ("/ARTICLE/DETIAL/4"); } PublicActionResult UrlTest5 () {//301 returnRedirecttoactionpermanent ("detial","article",NewSystem.Web.Routing.RouteValueDictionary (New{id =5 })); //return redirecttoactionpermanent ("Detial", "article", new {id = 1}); } PublicActionResult UrlTest6 () {//301 returnRedirecttoroutepermanent ("article",NewSystem.Web.Routing.RouteValueDictionary (New{id =6 })); //return Redirecttoroutepermanent ("article", new {id = 1});}
You can also set your own
PublicActionResult UrlTest7 () {//can be set return NewRedirecttorouteresult ("article",NewSystem.Web.Routing.RouteValueDictionary (New{id =7}),false) { }; } PublicActionResult UrlTest8 () {//can be set return NewRedirectresult ("/ARTICLE/DETIAL/8",false); }
Note that specifying a different view in view () is not a redirect
Public actionresult UrlTest9 () {// return View ("detial"null New9 }); }
The second code snippet and the method in the third code snippet are returned to the client in the form of a fourth snippet, and finally in the Response.Redirect method.
Several ways of MVC redirection