Asp.net core ASP.NET Core 3.1的正文为空
下面的代码是为了更好地理解而总结的,它与LOCALHOST完美配合,但是,当我将其放入IIS时,请求体总是空的。有人能帮我吗 客户端应用程序代码:Asp.net core ASP.NET Core 3.1的正文为空,asp.net-core,core,webapi,Asp.net Core,Core,Webapi,下面的代码是为了更好地理解而总结的,它与LOCALHOST完美配合,但是,当我将其放入IIS时,请求体总是空的。有人能帮我吗 客户端应用程序代码: login(userName: string, password: string): Observable<User> { const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); return this.h
login(userName: string, password: string): Observable<User> {
const headers = new HttpHeaders({
'Content-Type': 'application/json'
});
return this.http.post(`${environment.API_URL}/profiles/login`,
{ userName, password }, { headers }
).pipe(
tap((currentUser: User) => {
this.updateUser(currentUser)
.then(
() => {
console.log('currentUser login stored: ', AppSettings.currentUser);
},
error => console.error('Error storing currentUser login', error)
);
return AppSettings.currentUser;
}),
);
}
[Route("api/[controller]")]
[ApiController]
public class ProfilesController : ControllerBase
{
[HttpPost("login")]
public async Task<ActionResult> Login(LoginRequest request)
{
try
{
using (var Reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var sb = new StringBuilder();
sb.AppendFormat("ContentType: {0}\n", Request.ContentType);
sb.AppendFormat("Request: {0}\n", Request.ToString());
sb.AppendFormat("ContentLength: {0}\n", Request.ContentLength.ToString());
if (Request.IsHttps)
sb.AppendFormat("{0}\n", "HTTPS!");
var headers = String.Empty;
foreach (var key in Request.Headers)
headers += key.Key + "=" + key.Value + Environment.NewLine;
sb.AppendFormat("Headers: \n{0}\n", headers);
sb.AppendFormat("QueryString: {0}\n", Request.QueryString);
var text = await Reader.ReadToEndAsync();
sb.AppendFormat("Body: {0}\n", text);
return Ok(sb.ToString());
}
return Ok("OK");
}
catch (System.Exception ex)
{
return Unauthorized($"{ex.Message}: {ex.StackTrace}");
}
}
}
ContentType: application/json
Request: Microsoft.AspNetCore.Http.DefaultHttpRequest
ContentLength: 79
Headers:
Accept=*/*
Accept-Encoding=gzip, deflate, br
Cache-Control=no-cache
Connection=keep-alive
Content-Length=79
Content-Type=application/json
Host=loja.online
User-Agent=PostmanRuntime/7.22.0
Postman-Token=121f1927-c340-492f-a98b-0d6586ff32d8
QueryString:
Body:
使用邮递员也会发生同样的事情强>
尝试指定源:
public async Task<ActionResult> Login([FromBody] LoginRequest request) //Added [FromBody]
public async Task在IIS上托管时,您是否点击了Login
方法?是否可以检查来自浏览器网络选项卡的请求?此外,什么是LoginRequest
?对不起,在《邮递员我没有这个标签》中,你能在你的行动方法中添加[FromBody]到LoginRequest旁边,然后再试一次吗?你好@MarciusBezerra,你收到LoginRequest
,它会第一次读取正文,然后你用你的方法再次读取,所以你的身体是空的。对于多次阅读身体,你可以参考。你能分享一个可以重现你的问题的样本吗?谢谢!成功了!我只是不明白为什么[FromBody],因为我使用的是ApiController属性。有道理。ApiController属性只是修饰你的控制器。基本上是告诉你的应用程序这是一个api控制器,必须这样对待。我尝试添加[FromBody]属性,但对我无效。还有其他的修复方法吗?我唯一能帮上忙的就是看看你的代码。因为可能是正文没有提交到后端。。。