简体   繁体   中英

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.

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.

Here's the ForgotPassword action:

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

This issue is not in @Url.Action. This is because you deny access to this action. May be you have [Authorize] attribute on your Login controller. If you don't want to delete [Authorize] attribute you can allow action in you 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. It was redirecting to the login because of faulty validation logic, which I fixed.

Thank you all for the sensible suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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