Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 创建自定义URL路由,将管理部分与网站的其他部分分开_Asp.net Mvc_Url_Routes_Asp.net Mvc 5 - Fatal编程技术网

Asp.net mvc 创建自定义URL路由,将管理部分与网站的其他部分分开

Asp.net mvc 创建自定义URL路由,将管理部分与网站的其他部分分开,asp.net-mvc,url,routes,asp.net-mvc-5,Asp.net Mvc,Url,Routes,Asp.net Mvc 5,我想在ASP.NET MVC 5项目中创建一个具有恒定路径的自定义URL路由。 例如,我想让admin是一个常量。除此之外,我还有一些路由 之后,我想为浏览器中输入的admin/controller/action/定义一个策略,指向管理面板,否则如果URL中不存在admin/则指向常规页面。 为了实现这一目标,我在_ViewStart.cshtml中编写了一些代码,但需要进行一些改革 public static void RegisterRoutes(RouteCollection routes

我想在ASP.NET MVC 5项目中创建一个具有恒定路径的自定义URL路由。 例如,我想让admin是一个常量。除此之外,我还有一些路由

之后,我想为浏览器中输入的admin/controller/action/定义一个策略,指向管理面板,否则如果URL中不存在admin/则指向常规页面。 为了实现这一目标,我在_ViewStart.cshtml中编写了一些代码,但需要进行一些改革

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
           name: "Tag",
           url: "Tags/{tag}/{page}/{id}",
           defaults: new { controller = "Article", action = "Index", tag = (string)null, id = UrlParameter.Optional, page = @"/d" }
           );

        routes.MapRoute(
          name: "Tags",
          url: "Tags/",
          defaults: new { controller = "Tag", action = "Index" }
          );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            null,
            "Page{page}",
            new { Controller = "Article", action = "Index" },
            new { page = @"/d" }
            );
}
_ViewStart.cshtml:

@{
   if (HttpContext.Current.User.IsInRole("Administrator"))
   {
       // ??? need some codes for directing just to the /admin part
       Layout = "~/Views/Shared/_AdminLayout.cshtml";
   }
   else
   {
       Layout = "~/Views/Shared/_Layout.cshtml";
   }
}
您可以在MVC应用程序中使用管理员,这将有自己的路由。请查看此链接以获取有关的帮助。你的路线可能是这样的

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

非常感谢。我使用了@Darin Dimitrov的解决方案来管理布局。