Asp.Net MVC 路由 - 如何匹配整个 URL?

发布于 2024-08-08 15:33:30 字数 1014 浏览 8 评论 0原文

我正在尝试创建一个包罗万象的路由来跟踪联属字符串何时出现在 URL 中。关联代码由 x 后跟 int 标记,并且仅出现在 URL 末尾(但在查询字符串之前)。

我的想法是,我将提取联属 ID,进行一些日志记录,然后在没有联属 ID 的情况下对同一请求执行 301。

例如:

http://www.domain.com/x32
http://www.domain.com/x32/
http://www.domain.com/path/to/something/x32
http://www.domain.com/x32?query=string
http://www.domain.com/x32/?query=string
http://www.domain.com/path/to/something/x32?query=string
http://www.domain.com/path/to/something/x32/?query=string

我有这条路线,

routes.Add(new Route("{url}/x{affiliateExternalId}", new MvcRouteHandler())
{
     Defaults = new RouteValueDictionary(
      new { controller = "Home", action = "LogCookieAndRedirect" }
     ),
     Constraints = new RouteValueDictionary(new { affiliateExternalId = @"\d{1,6}" })
});

它只匹配

http://www.domain.com/path/x32
http://www.domain.com/path/x32/

我需要做什么来匹配所有内容并将查询字符串传递给控制器​​?我怀疑我应该使用一个 * 运算符,但我无法让它执行我需要的操作。

I'm trying to create a catch-all route to track when an affiliate string is in the URL. An affilite code is marked by an x followed by an int, and will only appear at the end of a URL (but before the query string).

The idea is that I will extract the affiliate ID, do some logging, then do a 301 to the same request without the affiliate ID.

For example:

http://www.domain.com/x32
http://www.domain.com/x32/
http://www.domain.com/path/to/something/x32
http://www.domain.com/x32?query=string
http://www.domain.com/x32/?query=string
http://www.domain.com/path/to/something/x32?query=string
http://www.domain.com/path/to/something/x32/?query=string

I have this route

routes.Add(new Route("{url}/x{affiliateExternalId}", new MvcRouteHandler())
{
     Defaults = new RouteValueDictionary(
      new { controller = "Home", action = "LogCookieAndRedirect" }
     ),
     Constraints = new RouteValueDictionary(new { affiliateExternalId = @"\d{1,6}" })
});

Which only matches

http://www.domain.com/path/x32
http://www.domain.com/path/x32/

What do I need to do to match everything and pass the query string to the controller? There is a * operator that I suspect I should use, but I can't get it to do what I need.

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

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

发布评论

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

评论(3

萌面超妹 2024-08-15 15:33:30

{*url} 将匹配整个路径,而不仅仅是一段路径。但是,由于它匹配整个路径,因此您无法匹配最后的联属会员 ID。该路线将匹配每个请求。您可以通过添加路由约束来检查 url 末尾是否有联属 ID 来解决此问题:然后,

routes.MapRoute(
    "WithAffiliate",
    "{*url}",
    new { controller="Home", action="LogCookieAndRedirect" },
    new { url = @"/x[0-9]+$" }
 );

您的操作将需要从 url 参数解析联属 ID本身。如果您可以选择修改 URL 结构,则可以匹配位于路径开头的 ID:

routes.MapRoute(
    "WithAffiliate",
    "x{affiliateExternalId}/{*url}",
    new { controller="Home", action="LogCookieAndRedirect" }
 );

{*url} will match the whole path rather than just one segment. However, since it matches the whole path, you can't then match the affiliate ID on the end. The route will match every request. You can get around that by adding a route constraint to check that url has an affiliate ID at the end:

routes.MapRoute(
    "WithAffiliate",
    "{*url}",
    new { controller="Home", action="LogCookieAndRedirect" },
    new { url = @"/x[0-9]+$" }
 );

Your action will then need to parse the affiliate ID from the url parameter itself. If you have the option to modify the URL structure, it would be possible to match the ID if it were at the start of the path:

routes.MapRoute(
    "WithAffiliate",
    "x{affiliateExternalId}/{*url}",
    new { controller="Home", action="LogCookieAndRedirect" }
 );
你好,陌生人 2024-08-15 15:33:30

如果我这样做,我会将其作为查询字符串中的值之一。

示例:

http//www.domain.com/path/to/something?affiliate=X32&query=string

问题是联营 ID 是可选。如果您将其放入路径中,您将根据联属 id 是否存在来更改路径的结构,因此您必须将每条路线执行两次(一次使用联属 id,一次不使用) 。

If I were doing it, I would make it one of the values in the query string.

Example:

http//www.domain.com/path/to/something?affiliate=X32&query=string

The problem is that the affiliate id is optional. If you put it in the path you will change the structure of the path based on whether or not the affiliate id is present, so you'll have to do every one of your routes twice (once with the affiliate id, and once without).

谎言月老 2024-08-15 15:33:30

如果您只定义了一条路由,则只能拥有此包罗万象的路由。但是,如果您有多个路由,那么您必须将 {affiliateExternalId} 参数添加到所有路由中(除非我遗漏了某些内容)。

我第二个罗伯特·哈维建议

You can only have this catch-all route if you have only one route defined. But if you have multiple routes, then you're going to have to add that {affiliateExternalId} parameter to all routes (unless I'm missing something).

I second Robert Harvey's suggestion.

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