MVC Controller performs redirection

Source: Internet
Author: User

The previous action method can return a view with a view that renders a non-default action method named name, in which case it can be redirected to the action method that renders the view instead of rendering the view by itself.

It's a little cumbersome to understand.

returnView (" Nice");//You can use ==>returnRedirecttoaction (" Nice"), orreturnView (" Nice","Good");==>returnRedirecttoaction (" Nice","Good"); The result is the same unless there are some data in Nice's action that need to be passed to Nice view

For this redirect, I still have some not quite understand "temporary redirect" and "permanent redirect" Redirecttoactionpermanent
I have to say that MVC's controller helper method is really a lot, a redirect incredibly divided into

1.redirecttoroute//directed to the routing system URL

2.redirecttoaction//directed to the action method

In fact, these two helper methods do the same thing, is to redirect the current route to another action method, and then go to render a view back to the user.

But now the problem is, if it is redirected, the browser will submit a new HTTP request, which means that we are going to the original request details of the method, meaning we can not access the data in this action method, if we want to save the redirect before

What to do with the data in the action method, then we can use the TempData feature, which is often magical, and unlike a global variable, application shutdown is released, it is marked for deletion after the first read, and then formally deleted after the request is processed. is not very nice, do not occupy much memory, but also can pass data across requests, but also can automatically clear the memory ~ ~ ~

Use the following method

?
1 2 3 4 5 6 7 8 9 10 public RedirectToRouteResult Index(){   TempDate["Nice"] = "Terry";   TempData["Reason"] = "中奖啦";   return RedirectToAction("Nice"); }public ViewResult Nice(){   Nice nice = new Nice{Name=TempData["Nice"],Reason=TempData["Reason"]};   return View(nice); }

If you don't want to let go of this cross-domain data for more than one, you can use

Tempdata.keep ("nice");

So your nice data can be retained again, the next time you read, the request will be completed or deleted, if you still do not want to be deleted, then I can only recommend you use the session ...

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.