0

Mine is simple scenario but I am confused on how routing works.

I have three controllers SearchHome, Home and Account.

Here is route config.

 routes.MapRoute("default", "{controller}/{action}/{name}",
            new { controller = "SearchHome", action = "Home", name = UrlParameter.Optional }, new[] { "AppName" }
            );

I do not want to show my controller name in URL. So, I take away controller in my above code and make it as below.

  routes.MapRoute("default", "{action}/{name}",
            new { controller = "SearchHome", action = "Home", name = UrlParameter.Optional }, new[] { "AppName" }
            );

Controller Name is not showing for SearchHome controller views but Home and Account Views are showing up at all.

What am I doing wrong ? Can I write a seperate route for each controller ?

Here is the same issue but The answer is not working for me.

ASP.NET MVC - Removing controller name from URL

Community
  • 1
  • 1
Chatra
  • 2,989
  • 7
  • 40
  • 73
  • If you remove `{controller}`, how would you expect to be able to ever redirect to a method in `HomeController` or `AccountController`? Its unclear what your expecting - why do you not want to display the controller name? –  Nov 10 '15 at 00:20
  • @StephenMuecke I do not want to show controller name because it makes url big. I update my post with the link – Chatra Nov 10 '15 at 00:24
  • If you don't provide the controller name, then there is no way the routing engine can determine which controller method you want to redirect to. It works in the first case (`SearchHomeController`) because you have specified a default value (`controller = "SearchHome"`) but you can only have one default. –  Nov 10 '15 at 00:31
  • @StephenMuecke Understood that. How to resolve my scenario ? – Chatra Nov 10 '15 at 00:32
  • You cant. You need to provide something to distinguish which controller your wan't to hit (the routing engine is not psychic). You can always define a specific route for each controller with a shortened name - e.g. `routes.MapRoute("Account", A/{action}/{name}", new { controller = "Account", action = "Index", name = UrlParameter.Optional });` so that `../A/Index` will hit the `Index` method of `AccountController` –  Nov 10 '15 at 00:36
  • @StephenMuecke if we can't, I am surprised how come 27 members made that answer as useful. – Chatra Nov 10 '15 at 00:40
  • Because it's for ONE controller method only - read the first line in the question - the text in brackets (but you want if for all controller methods which will not work) –  Nov 10 '15 at 00:43
  • I haven't tried this, but theoretically you should be able to do this. But you will have to write routing entries for each individual controller and two given controllers cannot have the same action names. You can use constraints parameter. I'm not going to put an answer as this is really not practical. – Amila Nov 10 '15 at 08:16
  • http://stackoverflow.com/questions/932611/how-do-i-add-a-mvc-route-to-a-specific-controller This answer helps me in some way – Chatra Nov 11 '15 at 00:11

0 Answers0