mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Web API HTTP method attributes result in unsupported HTTP method #260

Open friedr1c3 opened 11 years ago

friedr1c3 commented 11 years ago

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

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?

gzak commented 11 years ago

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

devmondo commented 11 years ago

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!"; }

gzak commented 11 years ago

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 :-(

devmondo commented 11 years ago

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

should be [HttpGet("")]

gzak commented 11 years ago

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).

mccalltd commented 11 years ago

@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.

devmondo commented 11 years ago

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

mccalltd commented 11 years ago

@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... :(

devmondo commented 11 years ago

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 ?

shivesh-pandey commented 11 years ago

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