多个页面的 MVC 5 属性路由错误
MVC 5 Attribute Routing Error for multiple pages
我运行这个错误试图处理多个路由:
Server Error in '/' Application.
The layout page "Login" could not be found at the following path: "~/Views/Login/Login".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The layout page "Login" could not be found at the following path: "~/Views/Login/Login".
这是我的路由配置代码:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Dashboard",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
这是我的 _viewstart 代码
@{
string CurrentName = Convert.ToString(HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"]);
dynamic Layout;
switch (CurrentName)
{
case "Login":
Layout = "~/Views/Shared/_LoginPageLayout.cshtml";
break;
case "Dashboard":
Layout = "~/Views/Shared/_Layout.cshtml";
break;
default:
//Admin layout
Layout = "~/Views/Shared/_Layout.cshtml";
break;
}
}
关于我可能在哪里走错了弯,我能得到一些指导吗?
在视图开始时只需使用 Layout = "~/Views/Shared/_Layout.cshtml";
删除这部分 => routes.MapRoute( name: "Login", url: "{controller}/{action}/{id}", defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional } );
只需尝试点击主页的索引页(当您的用户尝试访问您的基本站点时,它会自动使用路由 url)
然后过滤具有 controller 角色的用户,如果未通过身份验证,则将其重定向到登录。
不要试图在网站的早期构建中破坏框架:)
我运行这个错误试图处理多个路由:
Server Error in '/' Application.
The layout page "Login" could not be found at the following path: "~/Views/Login/Login". Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The layout page "Login" could not be found at the following path: "~/Views/Login/Login".
这是我的路由配置代码:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Dashboard",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
这是我的 _viewstart 代码
@{
string CurrentName = Convert.ToString(HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"]);
dynamic Layout;
switch (CurrentName)
{
case "Login":
Layout = "~/Views/Shared/_LoginPageLayout.cshtml";
break;
case "Dashboard":
Layout = "~/Views/Shared/_Layout.cshtml";
break;
default:
//Admin layout
Layout = "~/Views/Shared/_Layout.cshtml";
break;
}
}
关于我可能在哪里走错了弯,我能得到一些指导吗?
在视图开始时只需使用 Layout = "~/Views/Shared/_Layout.cshtml";
删除这部分 => routes.MapRoute( name: "Login", url: "{controller}/{action}/{id}", defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional } );
只需尝试点击主页的索引页(当您的用户尝试访问您的基本站点时,它会自动使用路由 url)
然后过滤具有 controller 角色的用户,如果未通过身份验证,则将其重定向到登录。
不要试图在网站的早期构建中破坏框架:)