RETURNURL在ASP.NET MVC [英] ReturnUrl in ASP.NET MVC

查看:240
本文介绍了RETURNURL在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个看起来像这样在我的应用程序中的登录链接:

I currently have a login link on my application that looks something like this:

<a href="/login?ReturnUrl=" + <%= Request.RawUrl %>>Login</a>

我想在登录页面处理POST命令下面的控制器动作:

I want to handle the POST command on the login page in the controller action below:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(string returnUrl)
{
    // Authenticate user

    return Redirect(returnUrl);
}

这里的问题是,如果RawUrl是一些与多个网址参数,如SomePage的参数1 = 1&安培;参数2 = 2及参数3 = 3?,那么被传递到登录行动RETURNURL在第一个符号后截断: SomePage的?参数1 = 1。

The problem here is if the RawUrl is something with multiple url parameters like "somepage?param1=1&param2=2&param3=3", then the returnUrl that gets passed into the Login action is truncated after the first ampersand: "somepage?param1=1".

我试过URL编码的RawUrl但似乎任何区别。看来,ASP.NET MVC框架这里将它们映射到控制器动作参数,从而结束了剥离附加URL参数,我想在我RETURNURL参数,看看之前的UrlDecoding网址参数。

I've tried UrlEncoding the RawUrl but that seem to make any difference. It seems that the ASP.NET MVC framework here is UrlDecoding the url params before mapping them to the controller action parameters, which ends up stripping off the additional url parameters I want to see in my returnUrl parameter.

有没有解决这个办法吗?我知道我可以只使用Request的和解析出我需要的价值,但我想我会看看是否有一个更清洁的方式第一。

Is there any way around this? I know I could just use Request.Path and parse out the values I need, but I thought I'd see if there was a cleaner approach first.

推荐答案

您很可能编码的链接不正确。是的,他们确实需要连接codeD。下面是我们如何做到这一点:

You're probably encoding the links incorrectly. Yes, they do need to be encoded. Here is how we do it:

<a href="<%= Url.Action("Delete", "TimeRecord", 
    new RouteValueDictionary(new { id = timeRecord.AltId, 
    returnUrl=ViewContext.HttpContext.Request.Url.PathAndQuery }) ) %>">

这篇关于RETURNURL在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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