简体   繁体   English

对于 OData 和 ASP.NET 内核,URL 太长

[英]URL too long for OData and ASP.NET Core

I've set up an OData service using ASP.NET Core Web API with OData ( Microsoft.AspNetCore.OData ).我已经使用 ASP.NET Core Web API 和 OData ( Microsoft.AspNetCore.OData ) 设置了 OData 服务。

My unbound function has one parameter accepting a string that can be very long: http://localhost:5000/odata/MyFunction(ids='a,b,c,d,e,f') .我的未绑定 function 有一个参数接受一个可能很长的字符串: http://localhost:5000/odata/MyFunction(ids='a,b,c,d,e,f')

This works fine if the length of ids doesn't get too big.如果ids的长度不太大,这可以正常工作。 However, when passing a too long string, the request fails with HTTP 400 .但是,当传递太长的字符串时,请求会失败并HTTP 400

I've already tried this - unfortunately, it didn't work.我已经尝试过了——不幸的是,它没有用。

How do I have to configure the app so that long requests are supported both in production ( Kestrel ) and dev ( IIS Express ) mode?如何配置应用程序以便在生产 ( Kestrel ) 和开发 ( IIS Express ) 模式下都支持长请求?

The problem is not the length of the URL itself.问题不在于 URL 本身的长度。 Instead, it is related to OData functions, which lead to a very long URL segment .相反,它与 OData 函数有关,这会导致很长的 URL On Windows, this seems to be limited to 260 characters.在 Windows 上,这似乎限制为 260 个字符。 So in my case, the URL segment MyFunction(ids='a,b,c,d,e,f') is simply too long.所以在我的例子中,URL 段MyFunction(ids='a,b,c,d,e,f')太长了。

I found two different approaches:我发现了两种不同的方法:

  1. Change a Windows Registry setting ( see here ).更改 Windows 注册表设置(请参阅此处)。 I didn't test it, but the comments in the other thread look promising.我没有测试它,但另一个线程中的评论看起来很有希望。
  2. Use Parameter Aliases .使用参数别名

I've chosen the second approach, so now my URL looks like this: http://localhost:5000/odata/MyFunction(ids=@p1)?@p1='a,b,c,d,e,f' . I've chosen the second approach, so now my URL looks like this: http://localhost:5000/odata/MyFunction(ids=@p1)?@p1='a,b,c,d,e,f' . The neat thing about this is that I hadn't to change anything in my OData configuration - it just works.这样做的好处是我无需更改 OData 配置中的任何内容——它就可以工作。

After a bit of testing, I reached the maximum query string length.经过一番测试,我达到了最大查询字符串长度。 I solved this by introducing a web.config file with the following content:我通过引入具有以下内容的web.config文件解决了这个问题:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxUrl="20100" maxQueryString="20000" />
      </requestFiltering>
    </security>
  </system.webServer>
  <system.web>
    <httpRuntime maxUrlLength="20100" maxQueryStringLength="20000" />
  </system.web>
</configuration>

The downside of the second approach is that Power BI Desktop doesn't seem to support this easily.第二种方法的缺点是 Power BI Desktop 似乎并不容易支持这一点。 I was not able to add a parameter alias within the simple UI when browsing an OData service.浏览 OData 服务时,我无法在简单 UI中添加参数别名。 However, when creating a Power BI Parameter with the long query string, I was able to compose the OData URL with the Power BI Parameter als Parameter Alias on my own.但是,当使用长查询字符串创建 Power BI 参数时,我能够自己使用 Power BI 参数 als 参数别名来组合 OData URL。

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

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