Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
}
like image 444
stoic Avatar asked Aug 27 '10 12:08

stoic


2 Answers

Now in MVC 5 this can be achieved by using Route Attribute.

[Route("Help/Me")]
[Route("Help/Me/Now")]
[Route("Help/Polemus")]
[Route("Help/Polemus/Tomorow")]
public ActionResult Index()
 {
    return View();
 }
like image 50
SantyEssac Avatar answered Oct 13 '22 20:10

SantyEssac


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" });
like image 40
SLaks Avatar answered Oct 13 '22 22:10

SLaks