Mato_Vilac
Mato_Vilac

Reputation: 87

ASP.NET WebApi Get method 405 not allowed

I have been struggling with this problem for a while. I have looked at every corner of google that i could find with no success. The problem is that on jQuery call for GET method in ASP.NET WebApi controller it returns 405 not allowed. I have found on stackowerflow and some other pages that people are talking about WebDAV problem and CORS, and I have tried a lot of possible solutions, but nothing worked for me.

Also I have to tell that my call to my POST method does just fine. This project is developed localhost, it's not online.

Now I stand confused in front of the wall and I need Your help, and I would appreciate any. Why 405, why?

The simplified code I use is listed below:

WebApiConfig

 public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

ApiController:

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class UserInfoController : ApiController
{        
    [HttpGet]
    public async Task<UserModel> Get(string Facebook_ID)
    {             
        UserModel user = new UserModel();   
        //some database await work
        return user;
    }

jQuery (ajax call for GET method):

   $.ajax({
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    url: 'api/UserInfo/' + data.Facebook_ID,
    type: 'GET',
    contentType: 'application/json;',
    //data: JSON.stringify(data.Facebook_ID),
    success: function (valid) {
        if (valid) {
            console.log("GET  HURAAAAY!!!" + JSON.stringify(valid));
        } else {
            console.log("GET DAMN" + valid);
        }
    }
});

Web.config(part of it):

  <system.webServer>
<modules>
  <remove name="WebDAVModule" />
</modules>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <remove name="WebDAV" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

Upvotes: 1

Views: 777

Answers (2)

Bradley
Bradley

Reputation: 455

The controller has other methods ? try using

[HttpGet]
[Route("api/UserInfo/{Facebook_ID}")
[ResponseType(typeof(UserModel))]
public async Task<UserModel> Get(string Facebook_ID)
{             
    UserModel user = new UserModel();   
    //some database await work
    return user;
}

Why this error (http 405) ?, it is about coalition in your routing, maybe other method has the same sign, check this for attribute routing: Attribute Routing in ASP.NET Web API 2

Upvotes: 1

Rodrigo Rocha
Rodrigo Rocha

Reputation: 23

I think you should annotate you GET method with [AllowAnonymous] Attribute, of course, a Post method can't receive the same attribute. I am using this page as guide: http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api

Upvotes: -1

Related Questions