【问题标题】:Dotnet Return Url Missing Parameterdotnet 返回 URL 缺少参数
【发布时间】:2015-02-26 15:59:18
【问题描述】:

这是一个令人头疼的问题:

我有一个 Dotnet 应用程序,它会在某个非活动时间段后将用户注销。使用相关 CSHTML 中定义的操作的 JavaScript 函数将用户发送到某个控制器方法,该方法将注销用户。

当 JavaScript 代码决定用户应该退出时,它使用以下行来执行此操作:

location.href = settings.actions.expireSession + '?returnUrlString=' + currentUrl;

其中 settings.actions.expireSession 定义为:

expireSession: '@Url.Action("Expire", "Session")'

进入 location.href 的返回 url 字符串如下所示:

http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84&caseId=173

这是正确的,与 url 动作组合在一起的整个字符串如下所示:

"/Session/Expire?returnUrlString=http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84&caseId=173"

我在相关方法的入口处设置了一个断点,但是到达名为“returnUrlString”的字符串参数中缺少“caseId”:

http://localhost:49574/?returnUrl=http%3A%2F%2Flocalhost%3A49574%2FReport%2FReportWithUserIdAndCaseId%3FuserId%3D84

因此,当我输入用户名和密码重新登录时,我会被重定向到以下网址:

http://localhost:49574/Report/EntrySummaryReportWithPatientIdAndVisitId?userId=84

因为缺少关键参数而失败。

我错过了什么明显的东西吗?在 Dotnet 的寻址/重定向系统的自动化背景中是否还有其他东西可能导致这种神秘的消失?

非常感谢大家阅读本文,并非常感谢贡献者! - 伊利亚

【问题讨论】:

    标签: javascript .net


    【解决方案1】:

    在发送到会话/过期页面之前,您需要对 URL 进行转义。

    有关在 Javascript 中对其进行编码的信息,请参阅上一个问题: Encode URL in JavaScript?

    在会话过期页面上设置 returnUrlString 后,您应该取消转义并引导用户访问它。

    问题是会话/过期页面的 URL 是: "/Session/Expire?returnUrlString=http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84&caseId=173"

    服务器看到的是: 页面:"/Session/Expire?,QueryString ReturnUrlString:returnUrlString=http://localhost:49574/Report/ReportWithUserIdAndCaseId?userId=84,QueryString CaseId:&caseId=173"

    它将您的 &caseId 解释为 /Session/Expire URL 的一部分。这就是它消失的原因。

    【讨论】:

    • 谢谢你,好心的先生。就是这样。作为记录,我尝试了“encodeURI(uri)”但失败了。有效的是“encodeURIComponent(uri)”。
    • 它往往会根据你想用它做什么而有所不同,很高兴这解决了你的问题。
    猜你喜欢
    • 2015-10-31
    • 2019-10-31
    • 2016-12-06
    • 2019-03-03
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多