【问题标题】:Using dash in the URL query in ASP.NET Core在 ASP.NET Core 的 URL 查询中使用破折号
【发布时间】:2018-10-27 14:34:54
【问题描述】:

我们可以在 ASP.NET Core 的 Route 模板中使用破折号 (-) 吗?

// GET: api/customers/5/orders?active-orders=true
[Route("customers/{customer-id}/orders")]
public IActionResult GetCustomerOrders(int customerId, bool activeOrders)
{
    .
    .
    .
}

(上面的代码不行)

【问题讨论】:

  • 但你为什么要这么做?
  • 是的,好问题。你为什么要首先这样做?路由参数通常直接映射到变量名,所以Route("customers/{customerid}/orders") 应该可以工作,因为那是你的变量名。不过你可以试试public IActionResult GetCustomerOrders([FromRoute(Name = "customer-id")]int customerId, bool activeOrders)
  • @Tseng - 1. “我们建议您在 URL 中使用连字符 (-) 而不是下划线 (_)。” support.google.com/webmasters/answer/76329?hl=en 2. 我试过你的解决方案,它有效。我也可以有..., [FromQuery(Name = "active-orders")] bool activeOrders)。极好的。问题解决了。为了其他人,您能否发布这 2 个 cmets 作为答案?
  • @AmanB - 建议在 URL 中使用破折号而不是驼峰式或连字符。见:stackoverflow.com/a/2318376/538387
  • @AmanB作为设计师我觉得好看多了,而且url就是客户看到的东西

标签: asp.net-core asp.net-core-mvc asp.net-core-2.1


【解决方案1】:

路由参数通常直接映射到操作的变量名,所以[Route("customers/{customerId}/orders")] 应该可以工作,因为这是你的变量名(int customerId)。

不需要破折号,花括号{}内的部分永远不会作为生成的url的一部分出现,它总是会被你从浏览器传递的内容替换或您传递给 url 生成器的变量。

customerId 设置为 1 时,customers/{customerId}/orders 将始终为 customers/1/orders,因此尝试将其强制为 {customer-id} 是没有意义的。

不过,你可以试试公开

[Route("customers/{customer-id}/orders")]
IActionResult GetCustomerOrders([FromRoute(Name = "customer-id")]int customerId, bool activeOrders)

如果您愿意,可以从非常规的路由名称绑定customerId。但我强烈建议您不要这样做,因为它只会添加不必要的代码,而这些代码对您生成的网址绝对零影响

上面生成(并解析)完全相同的网址

[Route("customers/{customerId}/orders")]
IActionResult GetCustomerOrders(int customerId, bool activeOrders)

而且是更易读的代码。

对于查询部分,正如您在 cmets 中发现的那样,通过 [FromQuery(Name = "active-orders")] bool activeOrders 添加破折号是有意义的,因为这确实会影响生成的 url。

ASP.NET Core 2.2 中的新功能

在 ASP.NET Core 2.2 中,您将获得一个“slugify”路由的新选项(仅在使用新的 Route Dispatcher 而不是默认的 Mvc 路由器时支持)。

blog\{article:slugify} 的路由(与 Url.Action(new { article = "MyTestArticle" }) 一起使用时)将生成 blog\my-test-article 作为 url。

也可以在默认路由中使用:

routes.MapRoute(
    name: "default",
    template: "{controller=Home:slugify}/{action=Index:slugify}/{id?}");

更多详情请参阅ASP.NET Core 2.2-preview 3 annoucement

【讨论】:

    【解决方案2】:

    只是扩展对问题的回答。要让 ASP NET CORE 使用 "slugify" 转换器,您需要先注册它,如下所示:

    public class SlugifyParameterTransformer : IOutboundParameterTransformer
    {
        public string TransformOutbound(object value)
        {
            if (value == null) { return null; }
    
            return Regex.Replace(value.ToString(), 
                                 "([a-z])([A-Z])",
                                 "$1-$2",
                                 RegexOptions.CultureInvariant,
                                 TimeSpan.FromMilliseconds(100)).ToLowerInvariant();
        }
    }
    

    然后在 Startup.cs 中

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
    
        services.AddRouting(options =>
        {
            options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
        });
    }
    

    Code from Microsoft

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 1970-01-01
      • 2014-06-26
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多