Joe.wang
Joe.wang

Reputation: 11793

WebAPI Url match Issue

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 }
           );

Upvotes: 1

Views: 780

Answers (1)

Pitchai P
Pitchai P

Reputation: 1317

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

Upvotes: 1

Related Questions