Several ways to redirect MVC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.