简体   繁体   中英

ASP.NET MVC 2: What's a more MVC like way to code this C# “Back” button?

This is the code I currently use:

<% Uri MyUrl = Request.UrlReferrer;
   if( MyUrl != null)
       Response.Write("<a href=\"" + MyUrl.PathAndQuery + "\">Back</a>"); %>

I would think there is a more "MVC Html helper" way to do a "Back" button. This may require more information about the routes already setup, a list of possible parameters, etc. but I don't want to assume this is the case. Maybe there is some way to match the url to the defined routes like in the manner a regular expression works?

What about adding the data you want to TempData, in an actionfilter or base controller class).

Each time when a page executes, it will add its info to the TempData, and the next page will know what the referrer was.The back button itself will be an HtmlHelper, that will simply go and fetch the data from the TempData.

This way you also eliminate the problem that some users don't send referrer info in the request.

well, for one you could always do what you did above, but inside a HtmlHelper.

As you pointed out, there's really too many variables to doing it based on routing, unless you havea known workflow (eg for a wizard).

I would suggest making it part of the view model, populated in your action. You could continue to use the referrer to populate it, or you could even have a "back url" parameter on your action, so that the calling page could supply its own back url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM