Web API POST data with error HTTP Error 405.0 - Method Not Allowed

1

I am writing a Web API to receive the telemetry data. The telemetry data in the form of hex string shall be POST to the web API URL. There is no authentication, no RoutePrefix and no Route needed for this Web API. The Content-Type: text/plain

Here is the my coding.

[Route("")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class MyData_Controller : ApiController
{
    [HttpPost]
    [ActionName("DefaultAction")]
    public Task<IHttpActionResult> DataPost(String myData)
    {
        SaveData(myData);
        return Task.FromResult<IHttpActionResult>(Json(true));
    }

    protected void SaveData(String myData)
    {
    }
}

then I hosted this WebAPI to the IIS8.5. In web.config, I also added the following

  <system.webServer>
    <handlers accessPolicy="Read, Execute, Script">
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <remove name="WebDAV" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." 
        verb="GET,HEAD,POST,PUT"
        type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Execute" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
  </system.webServer>  

When I test it using POST in Postman, I get the the following error

HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.

Detailed Error Information:
Module     DirectoryListingModule
Notification       ExecuteRequestHandler
Handler    StaticFile
Error Code     0x80070001
Requested URL      http://MyDataServer.com:2331/
Physical Path      E:\Applications\Web API\MyDataServer_WebAPI
Logon Method       Anonymous
Logon User     Anonymous

Postman header shows

Allow → GET, HEAD, OPTIONS, TRACE

Does it mean that verb POST or PUT are not enabled?

I checked StaticFile handler and it is configured in the Handler Mappings and Requested Restrictions -> Verbs has All verbs selected.

What is wrong with my implementation and site configuration?

c#
asp.net-web-api
asp.net-web-api-routing
asked on Stack Overflow Sep 8, 2017 by Santa Paws • edited Sep 15, 2017 by Nkosi

2 Answers

0

I kink of fixed the problem, which may not be the perfect way. Just put my solution here in case someone maybe benefited more or less.

if I put authentication attribute over the handler and within the authentication class, always return true (giving a fake authentication success), then the API works properly without having 405 error. I don't feel this is the right method, but really don't have time to further dig around.

the other issue is to have route defined. if route is not provided, then make this controller the Default controller in the route config part.

answered on Stack Overflow Sep 15, 2017 by Santa Paws
0

You can use [AllowAnonymous] on the controller or action to get around having to fake the authentication.

You can set the [Route] to be the root seeing as you are using attribute routing which gives you more control and greater flexibility when designing the URIs for your API.

Seeing as it is a web API you could simply use the HTTP Status Code to provided the feed back from the request.

If the telemetry data is only going to be sent via the body of the request then you should restrict it by using [FromBody] parameter binding attribute.

[Route("/", Name = "DefaultTelemetry")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class MyData_Controller : ApiController {
    [HttpPost]
    [AllowAnonymous]
    [ActionName("DefaultAction")]
    [Route("")] //Matches POST / <-- site root
    public async Task<IHttpActionResult> Post([FromBody]String data) {
        var saved = await SaveData(data);
        return saved ? Ok() : BadRequest();
    }

    protected Task<bool> SaveData(String data) {
        //...implementation
    }
}

Reference Attribute Routing in ASP.NET Web API 2

answered on Stack Overflow Sep 15, 2017 by Nkosi • edited Sep 15, 2017 by Nkosi

User contributions licensed under CC BY-SA 3.0