1

I was referencing this question to try and get this done, but it only works for my index method and I am not sure why.

My project has one area in it (if that is relevent) and I have about 5 different views that I want to hide /home/ in the url.

Code:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"JobResults",                                           // Route name
"JobSearch/{title}-{id}",                               // URL with parameters
new { controller = "JobSearch", action = "Job" },       // Parameter defaults
new[] { "inkScroll.Web.Controllers" }
);

routes.MapRoute("home", "{action}",
new { controller = "Home", action = "index" });


routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
    controller = "^(account|common|base|jobsearch)$", //every controller goes in here
    action = "Index", 
    id = UrlParameter.Optional
},
new[] { "inkScroll.Web.Controllers" }
);
halfer
  • 19,824
  • 17
  • 99
  • 186
ledgeJumper
  • 3,560
  • 14
  • 45
  • 92

2 Answers2

2

I am solving the same problem with the help of Attribute based Routing feature of ASP.NET MVC 5. Say, I have an action named ContactUs in my Home Controller as follows:

public ActionResult ContactUs()
{
  return View();
}

I used to get Url for ContactUs as /Home/ContactUs. But, I wanted it to be simply /ContactUs. So, I am decorting the ContactUs action with Route Attribute as follows:

[Route("ContactUs")]
public ActionResult ContactUs()
{

}

So, this is working perfectly for me. Therefore, if you are using ASP.NET MVC 5, I would say, utilize this excellent feature of MVC5. It will not only save you from separating Route Logic and Actions, but also, it solves many problems very easily.

If ASP.NET MVC5 is not an option for you, or if you dont want to use Route Attribute on Action Methods, then, perhaps the following route can work: ( I did not test it though)

routes.MapRoute("Default", "",
   new { controller = "Home", action = "index" });

This page contains helpful resource about Attribute Routing: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

Emran Hussain
  • 11,551
  • 5
  • 41
  • 48
  • Great call. I am using mvc5. I ended up with a similar solution, but I stuck the routes in the routing table rather than attribute routing. I think I will do this instead. Much cleaner. – ledgeJumper Apr 03 '14 at 14:35
0

Catch all wildcard route as the last one would work

routes.MapRoute("home2", "{*path}",
   new { controller = "Home", action = "index" });
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thanks for the answer. This is kinda working. It is taking me to the proper url (so, no /home in it) but I am getting the 'resource' cannot be found error. Is there something in the routing I am missing? I added the above code below my default route – ledgeJumper Apr 02 '14 at 22:48