2

I have two controller based on user type in my ASP.NET Core project. One is AdminController for admin and one is UserController for users. And there is also HomeController for signin and contact page. I am using following map route configuration for both admin and user controllers.

config.MapRoute(
      name: "UserRoute",
      template: "{controller}/{username}/{action}",
      defaults: new { controller = "User|Admin", action = "Dashboard" }
);

By using above route config I am getting following types of Urls

/User/user1
/Admin/user2

I don't want the Admin and User part in URL instead I want

/user1
/user2

How to remove User and Admin part from the URL? If I remove controller from {controller}/{username}/{action} and specify only controller in defaults then it only works for one controller.

Bhavesh Jadav
  • 695
  • 1
  • 13
  • 33
  • Check this similar problem: http://stackoverflow.com/questions/3337372/asp-net-mvc-removing-controller-name-from-url. You may utilize route constraint on additional `MapRoute` without affecting default route. – Tetsuya Yamamoto Oct 12 '16 at 09:42

1 Answers1

1

You can't have the same URL template with 2 default controllers, because mvc will not understand which controller to use.

You can either have 2 routes and each one with /Admin and /User, just like this:

config.MapRoute(
      name: "UserRoute",
      template: "User/{username}/{action}",
      defaults: new { controller = "User", action = "Dashboard" }
);

config.MapRoute(
      name: "AdminRoute",
      template: "Admin/{username}/{action}",
      defaults: new { controller = "Admin", action = "Dashboard" }
);

And from the Home controller, you can check the user role and redirect him to the correct route.

Another approach, would be only one route as you need

config.MapRoute(
      name: "UserRoute",
      template: "{username}/{action}",
      defaults: new { controller = "User", action = "Dashboard" }
);

But in this version, you will have only one controller and you can enable or disable actions according to the user role

Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19
  • When I use `RedirectToAction(actionname, controllername)`, am I not telling MVC which controller to use? Because I am specifying the controller while redirecting to page. I separated both `admin` and `user `in different controller because both of them have very few similar functionality and if I create only one `UserController` and maintain all types of user in that controller then it will be difficult to maintain. – Bhavesh Jadav Oct 12 '16 at 09:57
  • In the `RedirectToAction` you tell MVC which controller to use as it is a parameter and by the time the user is logged in, you will already know which controller to use, Admin to User. Any way, I see most of the sites have different URL for both User and Admin, and in MVC, I usually create an area for admin stuff and even maybe a separate master page. – Haitham Shaddad Oct 12 '16 at 10:15
  • I have different master pages for both `user` and `admin` in their respective view folders. I wanted to remove role from url because any one can know type of user just by examining the url. I guess I'll have to create only one `UserController` and handle user types in there but keeping different views and master page for all user types. – Bhavesh Jadav Oct 12 '16 at 10:26
  • You can remove the user /User from the user role and keep the /Admin only for the admin role since no one will see this role but the admin, this is what i normally do in any web application I develop – Haitham Shaddad Oct 12 '16 at 10:31