Question

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" }
);
Was it helpful?

Solution

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

OTHER TIPS

Catch all wildcard route as the last one would work

routes.MapRoute("home2", "{*path}",
   new { controller = "Home", action = "index" });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top