AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

Web API HTTP method attributes result in unsupported HTTP method

Open friedr1c3 opened this issue 12 years ago • 10 comments

Currently experiencing an issue with the HTTP method attributes that result in "The requested resource does not support http method '<method>'."

  • .NET 4.5.
  • ASP.NET Web API (latest).
  • IIS 7 (dev), IIS 8 (production).
  • AttributeRouting.WebApi (NuGet package).

Say I have the following code:

[RoutePrefix("api")]
public class TestController : ApiController
{
    [GET("test-method")]
    public string TestMethod()
    {
        return "Hello World!";
    }
}

The following route/URL does not work: /api/test-method

The result of the above produces the following error message: The requested resource does not support http method 'GET'.

However, say I have the following code:

[RoutePrefix("api")]
public class TestController : ApiController
{
    [GET("test-method"), HttpGet]
    public string TestMethod()
    {
        return "Hello World!";
    }
}

The following route/URL does work: /api/test-method because of the standard HttpGet attribute.

I am 100% sure I'm using the correct namespace of AttributeRouting for WebAPI; as noted in the documentation.

Namespace AttributeRouting.Web.Http

What has gone wrong? Is there a configuration issue somewhere?

friedr1c3 avatar Jul 24 '13 01:07 friedr1c3

I have the same issue, but it took quite a while to find this workaround.

gzak avatar Aug 26 '13 00:08 gzak

i don't know if it helps, but with WebApi the attributes should be [HTTPGET] and not [GET] so it should be

[HttpGet("test-method")] public string TestMethod() { return "Hello World!"; }

devmondo avatar Aug 26 '13 12:08 devmondo

I've got this controller:

public class BlahController : ApiController
{
    [HttpGet("/")]
    public string Hello()
    {
        return "hello world";
    }
}

I get nothing but a time out when I hit my localhost :-(

gzak avatar Aug 28 '13 05:08 gzak

try removing the forward slash, it is not needed for root url [HttpGet("/")]

should be [HttpGet("")]

devmondo avatar Aug 28 '13 13:08 devmondo

No dice. Let me post my code here, maybe you can try to it and see if it reproduces for you.

Program.Main method:

using (WebApp.Start<Startup>(url: "http://localhost:3141/"))
{
   Console.WriteLine("Press Enter to quit.");
   Console.ReadLine();
}

Startup.Configure method:

// Configure Web API for self-host.
var config = new HttpConfiguration();
config.Routes.MapHttpAttributeRoutes(cfg =>
{
    cfg.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
});

// Enable the application to use bearer tokens to authenticate users
appBuilder.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

// Enable the application to use a cookie to store information for the signed in user
appBuilder.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.ExternalAuthenticationType);
appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = CookieAuthenticationDefaults.ExternalAuthenticationType,
    AuthenticationMode = AuthenticationMode.Passive,
    CookieName = CookieAuthenticationDefaults.CookiePrefix + CookieAuthenticationDefaults.ExternalAuthenticationType,
    ExpireTimeSpan = TimeSpan.FromDays(1),
});

appBuilder.UseWebApi(config);

I'm using SelfHost, WebApi, and AttributeRouting in VS2013 preview (latest prerelease packages of everything).

gzak avatar Aug 29 '13 05:08 gzak

@friedr1c3 You need to either name your action method Get... or include the HttpGet attribute alongside the GET attribute. There are some workarounds needed for Web API support listed here: https://github.com/mccalltd/AttributeRouting/issues/96. Let me know if this helps.

These issues should be resolved once Web API vNext is released.

To everyone else: seems there is some confusion between the Web API attribute routing implementation and the impl in this project. It is never the case that AttributeRouting uses HttpGet("some/url"). That's a MS impl.

mccalltd avatar Aug 29 '13 14:08 mccalltd

@mccalltd does that mean we should always download attribute routing and use it even if new WbApi is implementing it ?

devmondo avatar Aug 29 '13 19:08 devmondo

@devmondo The new AttributeRouting from Microsoft is completely unrelated to this project, and is a ground-up implementation.

This is going to be confusing for folks... :(

mccalltd avatar Aug 29 '13 20:08 mccalltd

thanks for reply, and sorry for asking more, what is your advise? keep using attribute routing for Wep Api or use the Microsoft implemented one ?

devmondo avatar Aug 29 '13 22:08 devmondo

guys i am getting same issue , Please help me out.

shivesh-pandey avatar Oct 28 '13 15:10 shivesh-pandey