2

I'd like to create a custom URL routing that has a constant path at ASP.NET MVC 5 project. For example I'd like to have "www.mysite/admin/controller/action/" that admin is a constant.Also I have some routes apart from this.

After that I'd like to define a policy for while entered admin/controller/action/ in browser, directed to the admin panel, else if admin/ does not exist in URL, directed to the regular page. For this goal, I've written some codes in _ViewStart.cshtml but needs some reforms.

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";
   }
}
x19
  • 8,277
  • 15
  • 68
  • 126

1 Answers1

4

You could use an Admin Area in your MVC application, and this will have it's own routing. Take a look at this link here for help with Using Areas. Your route may look like this...

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • Many thanks.For managing the layouts I've used @Darin Dimitrov's solution. http://stackoverflow.com/a/5161384/1817640 – x19 Nov 07 '14 at 18:32