开发者

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

开发者 https://www.devze.com 2022-12-21 08:01 出处:网络
This is the code I currently use: <% Uri MyUrl = Request.UrlReferrer; if( MyUrl != null) Response.Write(\"<a href=\\\"\" + MyUrl.PathAndQuery + \"\\\">Back</a>\"); %>

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" wa开发者_运维百科y 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 (e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消