我有一个MVC网站,其中的访问权限是基于不同的角色控制的。一旦用户登录系统,他们可以看到导航到他们被授权的页面。然而,有些用户可能仍然尝试使用直接的URL访问某些页面。如果他们这样做,系统将自动重定向到登录页面。我希望将其重定向到另一个视图(未经授权)而不是登录页面。
Web.Config 文件中有以下条目:
<customErrors mode="On">
<error statusCode="401" redirect="~/Home/Unauthorized" />
<error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>
<authentication mode="Forms">
<forms name="Development" loginUrl="~/Account/Login" cookieless="UseCookies" timeout="120"></forms>
</authentication>
我已经在Global.asax.cs中注册了这些路由。
routes.MapRoute(
name: "Unauthorized",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Unauthorized", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "PageNotFound",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "PageNotFound", id = UrlParameter.Optional }
);
够用吗?