Search code examples
c#asp.net-mvc-2asp.net-mvc-routing

ASP.NET MVC multiple url's pointing to the same action


How do i map multiple url's to the same action in asp.net mvc

I have:

string url1 = "Help/Me";
string url2 = "Help/Me/Now";
string url3 = "Help/Polemus";
string url1 = "Help/Polemus/Tomorow";

In my global.asax.cs file i want to map all those url to the following action:

public class PageController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }
}

Solution

  • Add the following line to your routing table:

    routes.MapRoute("RouteName", "Help/{Thing}/{OtherThing}", new { controller = "Page" });
    

    EDIT:

    foreach(string url in urls)
        routes.MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });