Mvc redirection methods and mvc redirection Methods

Source: Internet
Author: User

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.

Related Article

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.