MVC3动态返回网址 [英] MVC3 Dynamic Return URL

查看:59
本文介绍了MVC3动态返回网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个视图,该视图的链接将设置为用户从中导航到该视图的任何页面的URL.

I want to have a View that has a link that will be set to the url of whatever page the user navigated to this view from.

比方说,我有一个名为InfoPage的视图和相对动作,在此页面上,我想要一个简单地表示返回"的链接.

Let's say I have a View and relative Action named InfoPage, and on this page I want a link that simply says 'Return'.

如果用户在PageA上并导航到InfoPage,则单击返回"链接会将用户返回到PageA.

If the user is on PageA and navigates to InfoPage, clicking the 'Return' link returns the user to PageA.

如果用户在PageB上并导航到InfoPage,则单击返回"链接会将用户返回到PageB.

If the user is on PageB and navigates to InfoPage, clicking the 'Return' link returns the user to PageB.

我认为最简单的方法是将"ReturnUrl"添加为InfoPage中使用的模型的属性.

I'm thinking the easiest means to do this will be to add the 'ReturnUrl' as a property of the model used in InfoPage.

我的问题是如何获取返回网址.

My question this is how do I get that return url.

    public ViewResult InfoPage(){
       var model = new InfoPageModel(); 
       //Set my model's other properties here...
       model.ReturnUrl = ''//Where do I get this?
       return view(model);
    }

然后在我看来

    <a href="@Model.ReturnUrl">Return</a>

推荐答案

最可靠的方法是将查询字符串参数从调用者页面传递到您的页面.指向此页面的每个链接都必须传递其自己的URL. (Request.Url).

The most robust way to do this is to pass a query-string parameter to your page from the caller page. Every link to this page will be required to pass its own URL. (Request.Url).

您还可以在控件中使用Request.UrlReferrer,但并非所有浏览器都发送Referer标头.

You could also use Request.UrlReferrer in your control, but not all browsers send Referer headers.

这篇关于MVC3动态返回网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆