【发布时间】:2017-09-23 02:43:50
【问题描述】:
如果用户登录我想显示我的部门视图,如果没有登录想显示登录页面。我在我的RouteConfig中尝试过这样的事情@
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
if (HttpContext.Current.User==null)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
else
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional }
);
}
}
但是这个总是在启动时加载登录页面。谁能指出我在这里做错了什么? 注意:我在这个应用程序中使用Asp.net Identity
【问题讨论】:
-
RegisterRoutes 不是每次调用都执行一次,而是每次应用启动一次。
-
@RoyDictus 有没有其他方法可以做我想做的事?
-
MSDN上的这篇文章你可能会感兴趣:blogs.msdn.com/b/webdev/archive/2013/10/20/…
-
如果您从 Visual Studio 2013 中的模板创建一个新的 MVC 应用程序,您几乎可以满足您的需求。并且不要检查
HttpContext.Current.User是否为空。这是不正确的,会给你错误的结果。
标签: c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity