开发者

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

开发者 https://www.devze.com 2023-01-13 16:01 出处:网络
How do i map multiple url\'s to the same action in 开发者_高级运维asp.net mvc I have: string url1 = \"Help/Me\";

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();
    }
}


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();
 }


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" });


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号