8

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();
    }
}
stoic
  • 4,700
  • 13
  • 58
  • 88
  • So is the strategy anything help goes to PageController? – Nix Aug 27 '10 at 12:38
  • more in the lines of...i have a list of random urls, and all random url's must map to the PageController. Note that the list is already defined... so i basically need to loop through the list and map each url in that list to the page controller – stoic Aug 27 '10 at 12:40

4 Answers4

30

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();
 }
Craig
  • 6,869
  • 3
  • 32
  • 52
SantyEssac
  • 789
  • 2
  • 19
  • 47
  • 2
    If you are reading this in 2017 or later, this answer is the way to go. Next, if you are wondering how to determine _which 1_ of the multiple routes brought you into this Action Method, you can use `ControllerContext.RouteData` and compare the URLs, [as explained beautifully in this other stackoverflow answer](http://stackoverflow.com/a/39626076/325521) – Shiva May 21 '17 at 18:54
  • 8
    In order to get the route attribute to work, you also need to add "routes.MapMvcAttributeRoutes();" in your RouteConfig.cs file before any of the other routes. [MSDN Blog Post](https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/) – joeshmoe301 Jun 15 '17 at 17:42
12

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" });
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • this is almost exactly as what i had... i tried your second example, however, when navigating to that url i get a "resource cannot be found" error, and i do have the associated "\Views\Page\Index.aspx" view. – stoic Aug 27 '10 at 12:49
  • hehe, with the edit, it's now exactly the same as my initial attempt :) – stoic Aug 27 '10 at 13:01
  • @Dusty: What are your URL strings? Make sure they have `/`, not \ (frontslash, not backslash). – SLaks Aug 27 '10 at 13:35
  • problem was i was mapping these routes after i mapped the default "/Controller/Action/Id" reoutes, i swapped it arround and now it works. thanx for confirming though – stoic Aug 27 '10 at 13:40
5

In my case I was looking to simply combine two 'hardcoded' routes into one and stumbled upon this post. I wanted to clean out my RouteConfig.cs a little - because it had so many similar routes.

I ended up using some simple 'or' logic in a regular expression and basically changed:

routes.MapRoute(
    "UniqueHomePage",
    "Default",
    new { controller = "Redirector", action = "RedirectToRoot" }
);

routes.MapRoute(
    "UniqueHomePage2",
    "Home",
    new { controller = "Redirector", action = "RedirectToRoot" }
);

Into a single route:

routes.MapRoute(
    "UniqueHomePageGeneric",
     "{url}",
      new { controller = "Redirector", action = "RedirectToRoot" },
      new { url = "Home|Default" }
);

Note for the SEO-savy or -interested: The reason for pointing multiple URL's to one and the same action, is to then redirect them to one and the same page again. In this case the homepage. So the idea is to prevent duplicate content issues. When you use this method for pointing for NON redirecting actions, but actions that show their own views, then you might be CAUSING duplicate content issues :P.

Bart
  • 5,065
  • 1
  • 35
  • 43
3

You can just add the routes into your route table as you need them, same as any other route. Just give them unique names, hard coded URL and point them to the same controller / action. It will work fine.

If you use pattern matching instead of hard coded URLs just make sure you get all your routes in the right order so the correct one is selected from the list. So /Help/Me should appear before /Help/{Page} if the hard coded route goes to a different page to the pattern matched one. If you put /help/{page} in the route tabel 1st this will match to /help/me and your hard coded named action for that route would never fire.

On a side note, if this is a public facing site and SEO is important please be careful if you have multiple URLs returning the same data, it will be flagged as duplicate. If this is the case, then use the Canonical tag, this gives all the page rank from all the URLS that go to that single page to the one you name and removes the duplicate content issue for you.

Paul Hadfield
  • 6,088
  • 2
  • 35
  • 56