简体   繁体   English

路由中的 .NET Web API 斜线

[英].NET Web API Slash in Route

I have a .NET Web API v2 and need to define a route that can contain forward slashes at any point in the route.我有一个 .NET Web API v2,需要定义一个路由,该路由可以在路由中的任何点包含正斜杠。 I have it configured (something) like this in WebApiConfig:我在 WebApiConfig 中配置了这样的(东西):

config.Routes.MapHttpRoute(
    name: "SomeRouteName",
    routeTemplate: "api/summary/{*token}",
    defaults: new { controller = "Summary" });

Unfortunately the token contains slashes and there's nothing I can do about it at this point.不幸的是,令牌包含斜杠,此时我无能为力。 The above route works in most cases if the slash is not at the beginning of the token.如果斜杠不在令牌的开头,则上述路线在大多数情况下都有效。 So that takes care of most cases.所以这可以处理大多数情况。 But if token begins with slash(es), it doesn't work because that first slash gets interpreted as part of the URL I assume and gets eaten.但是如果令牌以斜杠开头,它就不起作用,因为第一个斜杠被解释为我假设的 URL 的一部分并被吃掉。 So in my controller action, I have the following code (admittedly a hack that I'm trying to avoid):因此,在我的控制器操作中,我有以下代码(不可否认,这是我试图避免的 hack):

if (summary == null)
{
    summary = _repo.GetSummary($"/{token}");
}

Obviously, this will only work for a single slash.显然,这仅适用于单个斜杠。 I could do a loop and add more, but there isn't way to know how many it could be.我可以做一个循环并添加更多,但无法知道它可能有多少。 Currently no tokens in my DB begin with two slashes, so this bad code works for now.目前我的数据库中没有令牌以两个斜杠开头,所以这个糟糕的代码现在有效。 It was implemented as a band-aid until a better solution is found.它作为创可贴实施,直到找到更好的解决方案。

Edit: This references the * route, which mostly fixed my original issue, but still doesn't match the first slashes: URLs with slash in parameter?编辑:这引用了 * 路由,它主要解决了我的原始问题,但仍然与第一个斜杠不匹配: 参数中带有斜杠的 URL?

Since OP said in some comment:由于 OP 在一些评论中说:

There is no purpose -- but the tokens are generated and not necessarily by my code.没有任何目的——但令牌是由我的代码生成的,不一定是由我的代码生成的。 So I don't have control over the token generation.所以我无法控制令牌的生成。

and also:并且:

I've attempted to UrlEncode /Decode to see if this works, but the slash (encoded as %2F) still gets eaten for whatever reason.我已经尝试 UrlEncode /Decode 来看看这是否有效,但无论出于何种原因,斜线(编码为 %2F)仍然会被吃掉。 As a side note, I can say that Base64 encoding it will fix this but that I can't change this at this point because it would break the API for existing apps.作为旁注,我可以说 Base64 编码可以解决这个问题,但我现在不能改变它,因为它会破坏现有应用程序的 API。

I would say that one of best choices to avoid issues with special characters is firstly encoding the whole token as a base 64 string, and then url-encode it to encode possible characters like = .我想说避免特殊字符问题的最佳选择之一是首先将整个令牌编码为 base 64 字符串,然后对其进行 url-encode 以编码可能的字符,例如=

That is, you can configure a route where you don't need {token*} but just {token} .也就是说,你可以配置,你并不需要一个路线{token*}只是{token} If you need to simplify the token decoding, you can implement an ActionFilterAttribute that decodes the so-called bound parameter under the hoods.如果您需要简化令牌解码,您可以实现一个ActionFilterAttribute来解码ActionFilterAttribute下的所谓绑定参数。

Others have already done this way...其他人已经这样做了......

OAuth2 already sends basic authentication's credentials encoding them as a base64 string (you convert user:password to the whole base 64 string). OAuth2 已经发送了基本身份验证的凭据,将它们编码为 base64 字符串(您将user:password转换为整个 base 64 字符串)。

It's a common way of avoding these issues and base 64 strings can be decoded in every modern client and server platform.这是避免这些问题的常见方法,并且可以在每个现代客户端和服务器平台中解码 base 64 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM