简体   繁体   English

为什么我在 Asp.Net Core 日志中收到“不支持 POST 请求”?

[英]Why am I getting “POST requests are not supported” in my Asp.Net Core log?

I have an ASP.NET Core 5 API setup.我有一个 ASP.NET Core 5 API 设置。 I also have a few Razor pages that use static content.我还有一些使用静态内容的 Razor 页面。 I am using JWT for authentication.我正在使用 JWT 进行身份验证。 In my UserController I have an action stub where I am developing a change password action:在我的UserController我有一个动作存根,我正在开发一个更改密码动作:

    [Authorize]
    [HttpPost("ChangePassword")]
    public IActionResult ChangePassword([FromBody] string password)
    {
        HttpContext.Items.TryGetValue("User", out var user);

        _logger.ForContext<UsersController>().Debug("User {EmailAddress} tried to change his password to {Password}.", ((User)user).EmailAddress, password);

        return Ok();
    }

This works and logs as tested with Swagger.这可以正常工作并记录使用 Swagger 进行测试。 I have other POST actions.我还有其他 POST 操作。 This is the first time I try to get the user with HttpContext .这是我第一次尝试使用HttpContext获取用户。

However, only with this new Action, I get in my log two entries:但是,只有使用这个新操作,我才能在我的日志中获得两个条目:

23 Jul 2021 21:36:24.612 POST requests are not supported 2021 年 7 月 23 日 21:36:24.612 不支持 POST 请求
23 Jul 2021 21:36:24.605 POST requests are not supported 2021 年 7 月 23 日 21:36:24.605 不支持 POST 请求

The log (Seq) shows the SourceContext is Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware .日志 (Seq) 显示SourceContextMicrosoft.AspNetCore.StaticFiles.StaticFileMiddleware

I am wonder why I am getting these two log entries and can I safely ignore them?我想知道为什么我会收到这两个日志条目,我可以安全地忽略它们吗?

That log entry is coming from StaticFileMiddleware checking if it should take over the request and return a file response.该日志条目来自StaticFileMiddleware检查它是否应该接管请求并返回文件响应。

public class StaticFileMiddleware
{
    // ...
    public Task Invoke(HttpContext context)
    {
        if (!ValidateNoEndpoint(context))
        {
            _logger.EndpointMatched();
        }
        else if (!ValidateMethod(context))
        {
            // 👇 "POST requests are not supported"
            _logger.RequestMethodNotSupported(context.Request.Method);
        }
        else if (!ValidatePath(context, _matchUrl, out var subPath))
        {
            _logger.PathMismatch(subPath);
        }
        else if (!LookupContentType(_contentTypeProvider, _options, subPath, out var contentType))
        {
            _logger.FileTypeNotSupported(subPath);
        }
        else
        {
            // If we get here, we can try to serve the file
            return TryServeStaticFile(context, contentType, subPath);
        }

        return _next(context);
    }
}

Since it doesn't serve a static file, it calls the next middleware in the chain, which eventually lets the request be dispatched to your controller.由于它不提供静态文件,它调用链中的下一个中间件,最终让请求被分派到您的控制器。

You can safely ignore it.您可以放心地忽略它。 It's logged at Debug level and only matters if you're having problems with static files.它记录在Debug级别,仅当您遇到静态文件问题时才重要。

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

相关问题 ASP.NET Core Web API &amp; Entity Framework Core; 为什么我收到这个错误? - ASP.NET Core Web API & Entity Framework Core; why am I getting this error? 为什么 ASP.NET Core 不能并行处理我的请求? - Why does ASP.NET Core not process my requests in parallel? 为什么我在 ASP.NET Core 6 C# 中收到此错误? - Why am I getting this error in ASP.NET Core 6 C#? 为什么我没有使用c#在我的asp.net应用程序中获取.csv文件? - why i am not getting the .csv file in my asp.net application using c#? 为什么在ASP.NET中的Web配置中出现错误? - Why am I getting an error in web config in ASP.NET? 为什么我在asp.net网站启动时收到ThreadAbortException - Why am I getting a ThreadAbortException on asp.net website startup ASP.NET Core Web App 将 404 请求记录为警告 - ASP.NET Core Web App log 404 requests as warning 将重写规则添加到ASP.NET Core Web应用程序时,为什么会出现System.FormatException? - Why am I getting System.FormatException when adding rewrite rules to ASP.NET Core web application? ASP.NET Core - 使用 [HttpPost] 发布请求不适用于对象 - ASP.NET Core - Post requests using [HttpPost] not working for objects asp.net 3.5 我“得到”这个了吗? - asp.net 3.5 Am I “Getting” this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM