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

Controller Action Selection #223

Open smolesen opened 11 years ago

smolesen commented 11 years ago

Hi

In std. ASP.NET Web API, it's possible to have a controller like this:

public class ValuesController : ApiController
{
    public void GetByName(string name)
    {
     ...
    }

    public void GetByProdNo(string prodno)
    {
        ...
    }
}

and reach the methods using: ~/api/values?name=abc ~/api/values?prodno=123

If I do the same with AR:

[RoutePrefix("values")]
public class ValuesController : ApiController
{
    [GET(""), HttpGet]
    public void GetByName(string name)
    {
        ...
    }

    [GET(""), HttpGet]
    public void GetByProdNo(string prodno)
    {
        ...
    }
}

I only seem to be able to hit my controller with:

~/values?name=abc

~/values?prodno=123 gives an HTTP 404

is that intentionally?

TIA

/Søren

mccalltd commented 11 years ago

RoutePrefix prefixes only with what you specify. If you want things to start with "api" you've got to say so.

On Mar 18, 2013, at 5:38 AM, smolesen notifications@github.com wrote:

Hi

In std. ASP.NET Web API, it's possible to have a controller like this:

public class ValuesController : ApiController { public void GetByName(string name) { ... }

public void GetByProdNo(string prodno)
{
    ...
}

}

and reach the methods using: ~/api/values?name=abc ~/api/values?prodno=123

If I do the same with AR:

[RoutePrefix("values")] public class ValuesController : ApiController { [GET(""), HttpGet] public void GetByName(string name) { ... }

[GET(""), HttpGet]
public void GetByProdNo(string prodno)
{
    ...
}

}

I only seem to be able to hit my controller with:

~/values?name=abc

~/values?prodno=123 gives an HTTP 404

is that intentionally?

TIA

/Søren

— Reply to this email directly or view it on GitHubhttps://github.com/mccalltd/AttributeRouting/issues/223 .

smolesen commented 11 years ago

Hi

Sorry if I didn't explain good enough... This was not about the 'api' prefix, it's about only being able to reach the one action-method when using AR, whereas being able to reach both when using std. ASP.NET Web API....

/Søren

mccalltd commented 11 years ago

I see. Probably the default Web API routes work because it is using that generic route mapping and the web api bits consider method params when resolving actions. With AR, you end up with a route per action, and this specificity is causing your problem. I'd have to investigate more to find out exactly what's happening.

taschmidt commented 11 years ago

I think I'm looking for basically the same thing. I'd like to overload a route like so:

[GET("foo/{id:int}")]
public void FooByInt(int id)
{}

[GET("foo/{id}")]
public void FooByString(string id)
{}

But I'm getting a 404 too. Is that not possible?

tim

mccalltd commented 11 years ago

@taschmidt -- Those routes won't work as expected due to integration issues with Web API (pending vNext release). See here for more info.

The upshot is you need to do either this:

[GET("foo/{id:int}"), HttpGet]
public void FooByInt(int id)
{}

[GET("foo/{id}"), HttpGet]
public void FooByString(string id)
{}

or this:

[GET("foo/{id:int}")]
public void GetFooByInt(int id)
{}

[GET("foo/{id}")]
public void GetFooByString(string id)
{}
taschmidt commented 11 years ago

Total bonehead move on my part. I did have the verb on my methods, I just forgot to put them on my pseudo-code. My problem was even dumber, I had {id} in my template but the method parameter was called "key". Once I changed my template parameter to {key}, it worked just fine!