AttributeRouting
AttributeRouting copied to clipboard
Web API HTTP method attributes result in unsupported HTTP method
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?
I have the same issue, but it took quite a while to find this workaround.
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!"; }
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 :-(
try removing the forward slash, it is not needed for root url [HttpGet("/")]
should be [HttpGet("")]
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).
@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 does that mean we should always download attribute routing and use it even if new WbApi is implementing it ?
@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... :(
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 ?
guys i am getting same issue , Please help me out.