@manhnguyenv

Welcome to my blog!

Multiple operations with path in Web API 2

May 9, 2018 15:51

Multiple operations with path in Web API (edit)

https://stackoverflow.com/questions/36455390/multiple-operations-with-path-api-external-and-method-get-while-calling-an-e

When you do your get like this

Get api/External/{id}

web api is not sure whether to call the Get Method with no parameter or the Get Method with Parameter because of the default routing defined in your web api config

I would suggest using attribute routing to fix your problem

  [Route("api/External/Get")]
  public async Task<IEnumerable> Get()

  [Route("api/External/Get/{id}")]
  public async Task Get(int value) 

https://stackoverflow.com/questions/49056556/api-routes-multiple-operations-with-path

config.Routes.MapHttpRoute(
    name: "route2",
    routeTemplate: "api/{controller}/{organizationSys}/{id}",
    constraints: new { id = @"\d+" }   // Only matches if "id" is one or more 
);

config.Routes.MapHttpRoute(
    name: "route1",
    routeTemplate: "api/{controller}/{organizationSys}"
);

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}"
);

Categories

Recent posts