ASP.NET MVC 多个 url 指向同一操作

发布于 2024-09-16 09:35:33 字数 402 浏览 4 评论 0原文

我如何将多个 url 映射到 asp.net mvc 中的同一操作

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

在我的 global.asax.cs 文件中,我想将所有这些 url 映射到以下操作:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里°也失望 2024-09-23 09:35:33

现在,在 MVC 5 中,这可以通过使用 Route Attribute 来实现。

[Route("Help/Me")]
[Route("Help/Me/Now")]
[Route("Help/Polemus")]
[Route("Help/Polemus/Tomorow")]
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();
 }
-小熊_ 2024-09-23 09:35:33

将以下行添加到您的路由表中:

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

编辑

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

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" });
天邊彩虹 2024-09-23 09:35:33

就我而言,我只是想简单地将两条“硬编码”路线合并为一条,并偶然发现了这篇文章。我想稍微清理一下我的 RouteConfig.cs - 因为它有很多类似的路由。

我最终在正则表达式中使用了一些简单的“或”逻辑,并基本上进行了更改:

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

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

变成单一路由:

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

精通 SEO 或感兴趣的注意事项: 将多个 URL 指向一个相同操作的原因是然后再次将它们重定向到同一页面。在本例中为主页。所以我们的想法是防止重复内容问题。当您使用此方法指向非重定向操作,但显示其自己的视图的操作时,您可能会导致重复内容问题:P。

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.

〗斷ホ乔殘χμё〖 2024-09-23 09:35:33

您可以根据需要将路由添加到路由表中,就像任何其他路由一样。只需给它们唯一的名称、硬编码 URL 并将它们指向相同的控制器/操作即可。它会工作得很好。

如果您使用模式匹配而不是硬编码 URL,只需确保以正确的顺序获取所有路由,以便从列表中选择正确的路由。因此,如果硬编码路由转到与模式匹配的页面不同的页面,则 /Help/Me 应出现在 /Help/{Page} 之前。如果您将 /help/{page} 放在第一个路由表中,这将与 /help/me 匹配,并且该路由的硬编码命名操作永远不会触发。

顺便说一句,如果这是一个面向公众的网站,并且 SEO 很重要,请小心,如果您有多个 URL 返回相同的数据,它将被标记为重复。如果是这种情况,请使用 Canonical 标记,这会将转到该单个页面的所有 URL 的所有页面排名赋予您指定的页面,并为您消除重复内容问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文