Andrés Nava - .NET Andrés Nava - .NET - 1 month ago 32
AngularJS Question

405 Method Not Allowed on WebAPI2 (OWIN)

I am setting up a WebAPI endpoint for my API, but am having trouble getting my AngularJS calls to my PostRegister method to work.

The webAPI and Angular are on separate sites.

On the Startup.cs I have:

public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);

var config = new HttpConfiguration();

WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);

}

private void ConfigureOAuth(IAppBuilder app)
{
var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString(Paths.TokenPath),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = Kernel.Get<IOAuthAuthorizationServerProvider>()
};
app.UseOAuthAuthorizationServer(oAuthServerOptions);

OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
}


Then on the controller, I have my method that is set to allow anonymous:

[AllowAnonymous]
public bool PostRegister(UserSignupForm form)
{
...
}


I've also updated the web.config:

<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>


On the Angular side, I make a simple $http.post call:

saveRegistration: function (registration) {
return $http.post(baseUrl + '/api/signup/register', registration).then(function (response) {
return response;
});
}


When making the call using Postman, I get a successful response, but when doing the same call from Angular, I get the 405 error.

405 Method Not Allowed

BitOfTech sample site, is sending the same request headers, but is able to get the Access-Control-Allow-XXX headers

enter image description here

I've updated my code to follow Token Based Authentication using ASP.NET Web API 2, Owin, and Identity

Answer

I had similar issue recently. I added controller handler for the pre-flight OPTIONS request and it solved the problem.

[AllowAnonymous]
[Route("Register")]
[AcceptVerbs("OPTIONS")]
public async Task<IHttpActionResult> Register()
{
    return Ok();
}
Comments