![](/img/trans.png)
[英]Calling web api core asp.net from remote computer always gets 400 bad request
[英]ASP.NET Core 5, HttpPut request with parameters from body always returns Http 400 Bad request
首先,這是我在這個網站上的第一篇文章。 這也是我嘗試發布到 web 服務器上的第一個應用程序。 所以如果有些事情不是那么清楚,或者不是他們應該的那樣,請原諒我。
問題如下:當我向后端發送 PUT 請求時,總是收到“Http 400 Bad request”錯誤。 當我將后端上傳到 IIS 托管服務器時,出現了這個問題。 當我在 localhost 上運行應用程序時,問題不會發生。
我創建了一個簡單版本的 PUT 請求,如下所示:
[HttpPut("{id:int}")]
public ActionResult PutTest(int id, [FromBody]TestClass model)
{
return Ok(model.Prop1);
}
測試類如下:
namespace WebApi.Models.Accounts
{
public class TestClass
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
}
例如,當我在https://localhost:5000
上運行此代碼時,它工作正常。 但是當我將后端部署到我的域時,比如說https://www.mySite.app
,它總是返回一個錯誤的請求錯誤。
我已經嘗試進一步簡化代碼。 並注意到,當我只在下面的 PUT 操作中要求一個單一參數時,它工作正常。
[HttpPut("{id:int}")]
public ActionResult PutTest(int id)
{
return Ok(id);
}
我使用的web.config
文件非常簡單,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\WebApi.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess">
<handlerSettings>
<handlerSetting name="debugLevel" value="file" />
<handlerSetting name="debugFile" value=".\logs\aspnetcore-debug.log"/>
</handlerSettings>
</aspNetCore>
</system.webServer>
</location>
</configuration>
我正在使用 Postman 測試我的后端。 我能夠成功地向后端中的所有其他 Http 函數發送請求,但主體中帶有參數的 PUT 請求除外。
我被困在這幾天了,似乎找不到解決方案。 所以任何幫助將不勝感激。 所有其他 Http 類都可以正常工作,順便說一句,POST,GET,DELETE 等等。
您是否嘗試過使用 Fiddler 或其他東西在失敗時檢查原始請求/響應? 除了上述 S. 10 Brinke 的建議之外,這也將有所幫助。
These pages from Microsoft would also help you troubleshoot and capture the IIS logs https: //docs.microsoft.com/en-us/iis/troubleshoot/diagnosing-http-errors/troubleshooting-http-400-errors-in-iis https ://docs.microsoft.com/en-US/troubleshoot/aspnet/error-logging-http-apis
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.