Web api不支持POST方法

Bin*_*ose 31 c# asp.net asp.net-mvc asp.net-web-api

在我的web api控制器中,我有一个带有以下代码的功能

       [HttpPost]
        public HttpResponseMessage Post(string schooltypeName)
        {
            _schoolTypeService.RegisterSchoolType(schooltypeName);

            var message = Request.CreateResponse(HttpStatusCode.Created);

            return message;
        }
Run Code Online (Sandbox Code Playgroud)

当我与小提琴手打电话时,我收到了这个错误

{"Message":"The requested resource does not support http method 'POST'."}
Run Code Online (Sandbox Code Playgroud)

我的摆弄参数是

User-Agent: Fiddler

Host: myhost:8823

Content-Type: application/json; charset=utf-8

Content-Length: 26
Run Code Online (Sandbox Code Playgroud)

请求正文

{"schooltypeName":"Aided"}
Run Code Online (Sandbox Code Playgroud)

请求网址是

http://myhost:8823/SchoolType
Run Code Online (Sandbox Code Playgroud)

(我配置了网址,GET正在使用此网址)

这里有什么不对?

Kir*_*lla 52

将您的操作更改Post([FromBody]string schooltypeName)为默认字符串类型预计会来到Uri.

更新:
将您的身体更改"Aided"为目前您需要一个类来进行反序列化工作(例如:class School { public string SchoolTypeName { get; set; } }

  • 你的评论,"默认字符串类型预计将来到Uri",对我来说是新闻.我已经使用Web API多年了,但我想总是使用params中的类.我整个早上都把头发拉出来...希望它更加被宣传. (4认同)

Soh*_*N3N 14

请参阅控制器顶部的using命名空间,如果您使用的是System.Web.Mvc,则可能会出现此问题:

用这个:

using System.Web.Http;
Run Code Online (Sandbox Code Playgroud)


Ger*_*ius 10

问题归结为:

如果您在启动时注册的路线routes.MapRoute( 必须用您的邮政方法装饰[System.Web.Mvc.HttpPost]

如果您在启动时注册了您的路线,则Routes.MapHttpRoute( 必须使用以下方式装饰您的帖子方法[System.Web.Http.HttpPost]

如果您使用MapRoute()[System.Web.Http.HttpPost]它不会工作

如果您使用MapHttpRoute()[System.Web.Mvc.HttpPost]它不会工作

  • 除了他在 Post 调用中还需要 `[FromBody]`。 (2认同)

gra*_*der 6

对于未来的读者.我发现了这个问题..但在其他地方找到了(我的)答案.

我用下面看到的属性装饰了方法.

    [System.Web.Http.HttpPost]
    public MyOutputObject DoSomething([FromBody]MyInputObject args)
    {
        Console.Writeline(args.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

我的客户端代码(C#,控制台应用程序)的完整性.

    private static Task<MyOutputObject> MyClientCall(string baseAddress, MyInputObject args)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(baseAddress);
        client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));

           /* Your class name would be "MyEntityController" most likely */
        string serviceUrl = baseAddress + @"api/MyEntity/DoSomething";

        HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, args).Result;

        Console.WriteLine(response);
        Console.WriteLine(response.Content.ReadAsStringAsync().Result);

        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine("ERROR:  :(   " + response.ReasonPhrase);
            return null;
        }
        Task<MyOutputObject> wrap = response.Content.ReadAsAsync<MyOutputObject>();
        return wrap;
    }
Run Code Online (Sandbox Code Playgroud)

我在这里找到了答案:

http://blog.dontpaniclabs.com/post/2013/01/23/That-Pesky-Requested-Resource-Does-Not-Support-HTTP-Method-POST-Error-When-Using-MVC-Web-API