不允许使用Asp.Net Core 3.1 405方法 [英] Asp.Net Core 3.1 405 Method Not Allowed

查看:74
本文介绍了不允许使用Asp.Net Core 3.1 405方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助.我总是得到405 Method Not Allowed

我正在使用Asp.Net Core Web Application 3.1,我对HttpGet没问题,但是当我使用HttpPost时,它总是返回405状态代码

I'm using Asp.Net Core Web Application 3.1, I dont have problem with HttpGet but when i use HttpPost it always return 405 Status Code

这是我的控制器

[Route("api/[controller]")]
[ApiController]
public class ExamController : ControllerBase
{
    [HttpPost("PostValue")]
    public ActionResult<HttpResponseMessage> PostInfo([FromBody] PersonalInfo info)
    {
        string json = JsonConvert.SerializeObject(info);
        HttpClient client = new HttpClient();
        var response = client.PostAsync("https://sampleapi/receive", new StringContent(json, Encoding.UTF8, "application/json"));

        if (response.IsFaulted)
            return BadRequest(response);

        return Ok(response);
    }
}

这是我的启动课程

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddCors(c =>
        {
            c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStatusCodePages();

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

        app.UseCors(options => options.AllowAnyOrigin());
    }

此处是URL和结果的示例图片

推荐答案

查看所提供的图像,您使用chrome发出url请求,这是一个HTTP GET命令.因此,您的应用程序收到了HTTP GET命令,但是您的方法想要接受HTTP POST方法.这就是为什么它说不允许的方法"的原因.

Looking at the provided image, you use chrome to issue the url request, which is a HTTP GET command. So, your app got an HTTP GET command but your method wants to accept an HTTP POST method. That's why it says 'method not allowed'.

如果要尝试使用http命令,则需要一个Web测试工具,例如 PostMan .

If you want to try http commands, you need a web test tool such as PostMan.

这篇关于不允许使用Asp.Net Core 3.1 405方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆