我如何指定的链接登录表单一个返回URL? [英] How do I specify a return url for a link to the login form?

查看:96
本文介绍了我如何指定的链接登录表单一个返回URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

够简单了,它似乎,但事实证明并非是 - 主要是由于一个事实,即查看不可能知道你有哪些方式,通过模型和控制器那里。无论如何,它是一个需要解决的问题:

Simple enough, it would seem, but it turns out not to be - mainly due to the fact that the View can't possibly know which way through Model and Controller you got there. Regardless, it is a problem that needs a solution:

我有一个登录链接,这将用户带到一个表单输入用户名和密码。当用户点击提交,我想重定向到他正在查看的页面。这样做最简单的方法似乎是指定的URL当前页面的查询字符串( ...?RETURNURL = ... )和其他一切都已经建成。

I have a login link, that takes the user to a form to enter username and password. When the user clicks "submit", I want to redirect to the page he was viewing. The easiest way to do so seems to be specifying the url to the current page as a querystring (...?returnUrl=...) and everything else is already built.

但呈现链接时我在哪里找到我的看法,这个网址是什么?只呈现某个链接的网址 - 我自然,因为我不希望真正导入用户不能使用RedirectToActionResult。如何?

But where do I find this url from my view, when rendering the link? I naturally can't use a RedirectToActionResult as I don't want to actually transfer the user - only render the url in a link. How to?


编辑:

我现在已经开始在这个问题上的赏金,因此,我认为适当的澄清我的需求也是如此。

I have now started a bounty on this question, and therefore I see fit to clarify my needs as well.

我有一个在我的共享文件夹命名为Login.ascx用户控件。在这里面,我渲染ActionLink的到登录表单,它包括在我的母版的页脚。我想做到的是以下内容:

I have a UserControl named Login.ascx in my Shared folder. In it, I render an ActionLink to the login form, and it is included in the footer on my Masterpage. What I want to accomplish is the following:

在该ActionLink的呈现,查询字符串 RETURNURL 的一个以当前正在呈现的视图route 追加。如果这是完成,用户将被带回他/她与已建成的ASP.NET MVC框架的功能成功登录后查看的页面。

When the ActionLink is rendered, the querystring returnUrl is appended with the a route to the view that is currently being rendered. If this is accomplished, the user will be taken back to the page he/she was viewing after successful login with functionality that is already build into the ASP.NET MVC Framework.

究其原因,previous答案都不足以主要是他们没有提供一种方法来构建路径URL到当前视图即可。我不知道如何追加查询字符串,但我不知道如何找出什么把该字符串。

The reason the previous answers have not been sufficient is mainly that they have not provided a way to build the route url to the current view. I do know how to append a querystring, but I do not know how to find out what to put in that string.

为了纪念一个答案的的答案,我想一个方法来重新构造的当前显示视图的路线,从母版一个UserControl。

In order to mark an answer as the answer, I want a method to re-construct the route to the currently shown view, from a usercontrol in the masterpage.

推荐答案

该解决方案是使用HttpContext.Current.Request.RawUrl这样的:

The solution is to use HttpContext.Current.Request.RawUrl like this:

<%= Html.ActionLink("log on", "LogIn", new { controller = "User", returnUrl = HttpContext.Current.Request.RawUrl }) %>

或者从 MVC期货(Microsoft.Web一个扩展方法.Mvc.dll):

Or with an extension method from MVC futures (Microsoft.Web.Mvc.dll):

<%= Html.ActionLink<AccountController>(c => c.LogOn("name", "password", false, HttpContext.Current.Request.RawUrl), "login here")%>

ActionController的是MVC中默认的一个,但只需添加RETURNURL到自己。

ActionController is the default one in mvc but just add returnUrl to your own.

这篇关于我如何指定的链接登录表单一个返回URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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