1

I had implement AttributeRouting and WebApi in my web project. And I try to route a Url like http://localhost/apis/test?adminId=yyy to GetSomeInfo Action. But I encounter some trouble , the url http://localhost/apis/test and http://localhost/apis/test?adminId=yyy both route to Action GetEntity. It seems it doesn't recognize the parameter adminId, It thought there is no parameters in url. So It turns out go to GetEntity Action. Can anyone help me? thanks.

What I had done so far is looks like below, It doesn't work.

[RoutePrefix("apis/test")]
public class SampleController : ApiController
{


    [HttpGet]
    [GET("")]
    public string GetEntity([FromUri]string name = null, [FromUri]string id = null)
    {
        ....
    }

    [HttpGet]
    [GET("")]
    public string GetSomeInfo([FromUri]string adminId)
    {
        ....
    }

 }

Edited

The route map code in global.asax is below. and I found the Url http://localhost/api/Sample?adminId=2BD48CF9-95EB-48D2-A1B2-1AFA273E586D can be routed to the GetSomeInfo Action. The Url http://localhost/api/Sample without any parameters route to GetEntity. that is exactly what I want. My question is why the RoutePrefix and FromUri doesn't work?

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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


            routes.MapRoute(
               name: "Default",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
           );
Joe.wang
  • 11,537
  • 25
  • 103
  • 180

1 Answers1

1

Because, you need to define further routes in global.asax.cs. see this, Single controller with multiple GET methods in ASP.NET Web API

Community
  • 1
  • 1
Pitchai P
  • 1,317
  • 10
  • 22
  • Hi, Pitchai, Firstly please review my updated Post. And in my application, I used the `RoutePrefix` to match the url. Seems your mentioned solution is not in my case. thanks. – Joe.wang Jan 03 '13 at 12:30
  • Then, you can add route constraints to GetSomeInfo method like this: [GET("url/{id}")]. Now this will respond to , /url/1 (via GET) – Pitchai P Jan 04 '13 at 03:30
  • Thanks, But In my question, I don't know how to respond to the `/url?1`. Is it possible to make that ? – Joe.wang Jan 04 '13 at 05:37