简体   繁体   English

MVC @ Url.Action指向错误的动作?

[英]MVC @Url.Action directs to wrong action?

I have a Login page with a "Forgot Password" link, which is created using an <a> tag with an href attribute of @Url.Action("ForgotPassword", "Login", new { Area = "Admin" }) , and for some reason this link directs me to the login page instead of the ForgotPassword page. 我有一个带有“忘记密码”链接的登录页面,该链接页面是使用<a>标记创建的,该标记的href属性为@Url.Action("ForgotPassword", "Login", new { Area = "Admin" }) ,出于某种原因,此链接将我定向到登录页面,而不是ForgotPassword页面。

Does anybody have a clue as to why this is? 有人知道这是为什么吗? I have been investigating and found nothing strange in my project. 我一直在调查,在我的项目中没有发现任何奇怪之处。

Another note: the "Index" action on my Login controller gets hit, but the "ForgotPassword" action never does. 另一个注意事项:我的Login控制器上的“ Index”操作被命中,但是“ ForgotPassword”操作从不执行。

Here's the ForgotPassword action: 这是ForgotPassword操作:

[HttpGet]
public ActionResult ForgotPassword()
{
    return View();
}

This issue is not in @Url.Action. @ Url.Action中没有此问题。 This is because you deny access to this action. 这是因为您拒绝访问此操作。 May be you have [Authorize] attribute on your Login controller. 可能您的登录控制器上具有[Authorize]属性。 If you don't want to delete [Authorize] attribute you can allow action in you web.config: 如果您不想删除[Authorize]属性,则可以在web.config中允许操作:

<configuration>
    ...

    <location path="Login/ForgotPassword">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>

...
</configuration>

如果Login控制器中有[Authorize]属性,请将其移动并放入您希望用户可以访问isAuthorized

It turns out my issue was being caused by my BaseController.cs which all of my other controllers inherit from. 事实证明,我的问题是由我的所有其他控制器继承的BaseController.cs引起的。 It was redirecting to the login because of faulty validation logic, which I fixed. 由于验证逻辑错误,我将其重定向到登录名,该问题已修复。

Thank you all for the sensible suggestions. 谢谢大家的明智建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM