【问题标题】:ReturnUrl in RouteConfig.Cs not workingRouteConfig.Cs 中的 ReturnUrl 不起作用
【发布时间】:2015-07-26 16:53:41
【问题描述】:

在我的 MVC 项目中,我在 RouteConfig.Cs 中使用此代码

routes.MapRoute(
                name: "Signin",
                url: "login/{ReturnUrl}",
                defaults: new { controller = "Account", action = "Signin", ReturnUrl = UrlParameter.Optional }
            );

但是当我从另一个页面浏览登录页面时,url 不能正常工作,例如:

localhost:43773/account/signin?ReturnUrl=anotherpage

我希望:localhost:43773/login/anotherpage

我该如何解决这个问题?

【问题讨论】:

  • AFAIK,你不能。这是因为两者都被视为等价物。
  • 我编辑我的帖子,我期望:localhost:43773/account/login/anotherpage
  • localhost:43773/account/signin?ReturnUrl=anotherpage 与该路由不匹配。您指定的路由以“login/”开头,并且有一个参数
  • 我编辑我的帖子,我期望:localhost:43773/login/anotherpage
  • 这是错误的做法,你会丢失你的查询字符串值,通常 ReturnUrl 编码的查询字符串值必须在重定向后保留。您是否有计划正确转发查询字符串?

标签: asp.net-mvc asp.net-mvc-5.2


【解决方案1】:

我了解您的期望。但是 ASP.NET MVC 不会根据您定义的路由生成 URL。这既是不必要的,也会产生开销。

如果拥有像 localhost:43773/account/login/anotherpage 这样的 URL 对您来说真的很重要,那么您可以查看 IIS URL Rewrite Module

这是一个示例,说明如何实现您想做的事情

<rewrite>
    <rules>
        <rule name="Standardize Log In Url" stopProcessing="true">
            <match url="^account/signin?ReturnUrl=([0-9a-z]+)" />
            <action type="Rewrite" url="account/login/{R:1}" />
        </rule> 
    </rules>
</rewrite>

注意此解决方案未经测试,因此您可能需要对其进行一些修改。

【讨论】:

  • thanx Parth 的回答,我预计:localhost:43773/login/anotherpage 并且我编辑了我的帖子,我该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
相关资源
最近更新 更多