edocetirwi
edocetirwi

Reputation: 548

Many ASP.net WebAPI routes

Just starting a project which is going to use a lot of WebAPI endpoints and had some questions about the routes. Since there are going to be many methods with different parameter names, the solution I've thought about is adding different routes with varying parameter names.

The problem is all the methods I define in various ApiController classes have the signature similar to

public string SomeMethod(string token, string id);
{
    //method body
}

I'd like to have other methods with:

public string SomeMethod1(string token, string logType)
{
    //method body
}
public string SomeMethod2(string token, string name)
{
    //method body
} ....etc

I would like to avoid having to define every method with the parameter name as "id", so that the routes would match and would bind to the respective method in the ApiController class.

Is this an acceptable practice to add many routes in WebAPI routes config, so that varying parameters with different parameter name would bind to the correct method.

Would it affect the overall performance, if I've many routes in the config class?

Is there a better way to achieve what I'm trying to pull here?

Upvotes: 0

Views: 173

Answers (1)

Colorado Matt
Colorado Matt

Reputation: 349

It sounds like your main concern is the trade off between the a) need for multiple route definitions versus b) a single route with the 'id' parameter name. While I doubt the performance hit of many routes is a big deal I would lean toward a single route definition for the sake of having less code. You don't have to call the parameter 'id', but it would need to be the same. Perhaps something generic like 'argument':

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{token}/{argument}",
    defaults: new { controller = "Blah", action = "SomeMethod" });

Upvotes: 1

Related Questions