c# - 如何解决 HTTP 错误 404.15 - 未找到?
问题描述
我在 .NET core 2.2 应用程序中工作。调用 API 时出现 HTTP 错误 404.15。
因为我在 URL 中传递了大量的项目。
而且我无权更改 IIS,如下所述。
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxQueryString="3000" maxUrl="1000" /> /* Change the Url limit here */
</requestFiltering>
</security>
</system.webServer>
</configuration>
我也尝试了以下解决方案,但它不起作用。
[HttpPost]
[RequestSizeLimit(40000000)]
public async Task<IActionResult> UploadFiles(IFormFile file)
{
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 52428800; //50MB
});
}
app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), appBuilder =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = null;
//TODO: take next steps
});
这是我的 API 调用
https://localhost:44473/ControllerName/InsertProduct?ProductSubCategoryId="4"&ProductName="Canon EOS 200D II DSLR Camera EF-S 18 - 55 mm IS STM and 55 - 250 mm IS STM (Black)"&ProductDescription="Is photography one of your passions? Bring home this EOS 200D II from Canon. This is Canon’s lightest DSLR that features a vari-angle LCD touch screen. It features a 24.1-megapixels APS-C CMOS sensor and a DIGIC 8 processor that capture stunning images. The EOS 200D II also has a lot of other features that make everyday photography a lot easier."&ProductSpecification="{"Specification":[{"In The Box": "1 Camera Body, 18 - 55 mm Lens, 55 - 250 mm Lens, Battery, Battery Charger, USB Cable, Camera Bag"},{"Model Number": "EOS 200D II"},{"Model Name": "eos200dii"},{"SLR Variant": "EF-S 18 - 55 mm IS STM and 55 - 250 mm IS STM"},{"Effective Pixels": "24.1 MP"},{"Image Sensor Size": "22.3 x 14.9"},{"Sensor Type": "CMOS"},{"Lens Mount": "EF Mount"},{"Shutter Speed": "1/4000 - 30 sec"},{"Image Format": "JPEG, RAW, C-RAW, RAW + JPEG, C-RAW + JPEG"},{"Video Resolution": "1920 x 1080"},{"Video Quality": "Full HD"},{"Battery Type": "Lithium"},{"Weight": "654g"}]}"&ProductOptions="{"Options":[{"Bank Offer":"10% Instant discount with HDFC Bank PayZapp Card on purchase of Rs.300 or more"},{"No Cost EMI": "Avail No Cost EMI on select cards for orders above ?3000"},{"Partner Offers":"Get FLAT 5% BACK with Amazon Pay ICICI Bank Credit card card for Prime members. Flat 3% BACK for non-Prime members. No minimum order value. No upper limit on cashback."}]}"&ProductPrice="57499"&ProductBrand="Canon"&IsActive="True"&Quantity="50"&ImageURL="NULL"
在控制器中调用方法
public int InsertProduct(Product product)
{
Dictionary<string, object> keyValues = this.GetProperty<Product>(product);
int i = bl.AddProduct<Product>(keyValues);
return i;
}
所以任何人都可以建议我一个更好的想法来解决这个问题。
解决方案
解决此问题的最简单方法是使用 POST 调用 InsertProduct ... 这样查询字符串限制不适用。
您在控制器中的操作应如下所示:
[HttpPost]//this is to accept a POST rather than a GET
public int InsertProduct([FromBody]Product product)
{
Dictionary<string, object> keyValues = this.GetProperty<Product>(product);
int i = bl.AddProduct<Product>(keyValues);
return i;
}
另请注意,我将[FromBody]添加到 Product 参数中。这样,控制器将期望 json 在请求的正文中,而不是在 url 中,也不来自表单集合。
此外,您需要更改 api 调用以使用 POST 而不是 GET。因为您没有显示进行 API 调用的代码.. 我无法帮助您将其更改为使用 POST..
如果您发布进行 API 调用的代码,我将更新此答案以向您展示如何使用 POST。
最后说明: MaxRequestBodySize 与您的问题无关,因此更改它毫无意义......
设置的限制是针对查询字符串和URL而不是请求的正文...
只需查看配置文件中的这一行:
<requestLimits maxQueryString="3000" maxUrl="1000" /> /* Change the Url limit here */
您会看到maxQueryString和maxUrl都影响 URL 大小而不是请求的正文。
正文是 POST 或 PUT 期间请求中的内容...
事实上,GET 请求没有正文......如果他们这样做,服务器会忽略它。
因此,通过在 POST 中发送数据,您将不会受到该限制的影响。
推荐阅读
- jpa - 为什么 JPA 重复持久方法不抛出异常?
- python - 函数不传递通过“for”循环接收到的值
- sql - 如何使用 SQL 按另一列中的值聚合一列中的数据
- image - 如何使用 Python 去除数字文本图像上的手写标记
- hive - 我安装了 Hadoop 3.2.0。和 Hive 并出现以下错误
- ruby-on-rails - 即使创建了带有参数的@user,也无法找到没有 ID 的用户
- wpf - 基于标准的点网(wpf)中两个网格的一对多映射
- centos - 如何修复 CentOS 上 Vysor 的插件问题
- javascript - 为什么我的变量在浏览器中不起作用,如何使它们成为全局变量?
- c# - 列出根目录下的所有文件