简体   繁体   中英

ASP.NET API Post method not working after publishing on the host

I'm trying to call my API using POST method, and it suppose to return (Status = "True") in JSON format.

THe API works fine when I'm call it from my device, but when I publish it to the host it always return a message says: (No HTTP resource was found that matches the request URI).

My Code is:

Model:

public class CbActions
{
    public string Status { get; set; } = "";
}

Controller

    [Route("api/CBController/CbSts")]
    [HttpGet, HttpPost]
    public List<CbActions> CBStatus(Data d)
    {
        if (d != null)
        {
            string Id = d.ID;
            string date = d.Date;
            string name = d.Name;
        }
        

        List<CbActions> output = new List<CbActions>();

        
        output.Add(new CbActions { Status = "True"});

        return output;
    }

Web.config:

<system.web>
<webServices>
  <protocols>
    <add name="HttpGet" />
    <add name="HttpPost" />
  </protocols>
</webServices> 
</system.web>

Also:

<system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>

</system.webServer>

I called my API from my device like this: http://localhost/CBAPI/api/CBController/CbSts

Also I calling it from the Host in the same way: https://xxxx.com/CBAPI/api/CBController/CbSts

Could you please tell me what I'm missing or Where I did a mistake???

Thanks

The issue lies with decorating your action method with [HttpGet, HttpPost] .

An action should only ever have a single verb attribute applied.

In the rare case your action does need to accept multiple verbs then you should use:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]

However I'd suggest you create a separate action method for each.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM