0

I followed the answer to this question but when I apply it I get an error if I try to access views under other controllers.

If I go to http://mydomain/MyActionUnderHome it works fine, but if I go to http://mydomain/SomeOtherController/MyAction it throws

"The resource cannot be found."

Shouldn't the Default route take over if the URL doesn't match the route definition above the Default route?
Are there perhaps new ways in MVC 5 to do this?


My routes:

routes.MapRoute(
    "HomeRoute",
    "{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "AccountRoute",
    "{action}/{id}",
    new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Community
  • 1
  • 1
  • @StephenMuecke Added them :) –  Jul 20 '15 at 14:00
  • `http://mydomain/SomeOtherController/MyAction` matches the first route definition - it equates to ,`SomeOtherController()` method in `HomeController` (with a parameter `id="MyAction"`). Since `SomeOtherController()` probably does not exist in `HomeController` you get the error. In addition, our second route can never be hit because of the first one is identical –  Jul 20 '15 at 14:09
  • Check the following link will help [How to remove the controller name from the url using rout in MVC](https://stackoverflow.com/questions/34650332/how-to-remove-the-controller-name-from-the-url-using-rout-in-mvc/34653793#34653793). – Kashif Saeed Jan 07 '16 at 11:44
  • https://stackoverflow.com/questions/57799273/how-to-remove-controller-name-from-url-in-mvc-project/57799331#57799331 – Pritesh Sep 07 '19 at 04:51

2 Answers2

3

Better to use Route attribute.

public class HomeController : Controller 
{
  [Route("")]
  public ActionResult Index() { return View(); }
  [Route("MyAccount")]
  public ActionResult Account() { return View(); }

}

Then add this line t your RouteConfig class before the routes.MapRoute

routes.MapMvcAttributeRoutes();
dan
  • 522
  • 6
  • 15
1

The issue is that you have is conflicting routes that the router can't resolve correctly

routes.MapRoute(
    "HomeRoute",
    "{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "AccountRoute",
    "{action}/{id}",
    new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);

When the router looks at the path /youraction/yourparameter it will resolve to the first route.

The next part of this is that when you provide two values as your URL (as in your example the default router never gets called because the router interprets your input as /action/id rather than controller/action/id

If you change your routes to be:

routes.MapRoute(
    "HomeRoute",
    "index/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Then you'll get your effect. But your second route can still never be called, as your scheme isn't correct.

mikeswright49
  • 3,381
  • 19
  • 22