Mvc redirection methods and mvc redirection Methods
Add a simple route in RouteConfig
// Add routes. mapRoute (name: "Article", url: "Detial/{id}", defaults: new {controller = "Article", action = "Detial", id = UrlParameter. optional}, constraints: new {id = @ "\ d +"} // namespaces: new string [] {});
302 redirection
public ActionResult UrlTest1() {//302 return Redirect("/Article/Detial/1"); } public ActionResult UrlTest2() {//302 return RedirectToAction("Detial", "Article", new System.Web.Routing.RouteValueDictionary(new { id = 2 })); //return RedirectToAction("Detial", "Article",new { id = 1}); } public ActionResult UrlTest3() {//302 return RedirectToRoute("Article", new System.Web.Routing.RouteValueDictionary(new { id = 3 })); //return RedirectToRoute("Article", new { id = 1 }); }
301 redirection
public ActionResult UrlTest4() {//301 return RedirectPermanent("/Article/Detial/4"); } public ActionResult UrlTest5() {//301 return RedirectToActionPermanent("Detial", "Article", new System.Web.Routing.RouteValueDictionary(new { id = 5 })); //return RedirectToActionPermanent("Detial", "Article", new { id = 1 }); } public ActionResult UrlTest6() {//301 return RedirectToRoutePermanent("Article", new System.Web.Routing.RouteValueDictionary(new { id = 6 })); //return RedirectToRoutePermanent("Article", new { id = 1 }); }
You can also set it yourself.
Public ActionResult UrlTest7 () {// you can set return new RedirectToRouteResult ("Article", new System. web. routing. routeValueDictionary (new {id = 7}), false) {};} public ActionResult UrlTest8 () {// return new RedirectResult ("/Article/Detial/8 ", false );}
Note that specifying different views in View () is not a redirection
public ActionResult UrlTest9() {//200 return View("Detial", null, new { id = 9 }); }
The methods in the second and third code segments are returned to the client in the form of the fourth code segment and finally as the Response. Redirect method.