0

I am working on a Web Api 2 project and I am using attribute based routing. Here is a sample route:

[Route("{id:int}", Name = "GetEmployeeById")]
[HttpGet]
public IHttpActionResult GetEmployee(int id)
{
    ...
}

This works with the following URLs:

  • ://host/employee/12345
  • ://host/employee?id=12345

What I would prefer is that the first form (the parameter in the URI), would not be allowed, and only the second form (query string) would work.

What I've Tried

Mostly, I've tried searching the web for a way to make this work, and I'm not finding much.

This page talks about route constraints but this syntax doesn't seem to work (anymore?).

This page doesn't actually prevent the URI form from working.

0

3 Answers 3

1

There is an attribute called "[FromUri]" that you can use to decorate a method parameter, and the model binder will try to look for that parameter from the Querystring, it may not help you with this scenario but it is good to know about it, so in case you want to pass a search options for example to a Get method.

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

[FromUri] appears to force a parameter that would normally be taken from the POST from the query string instead. It's not helpful for this question, but I did find it interesting; thanks!
0

Couple of ways to achieve this. Here are some options

  1. Rename parameter to something else than id (eg. employeeId).
  2. Change the default routing configuration in WebApiConfig:

        //Default configuration, you can see here the "id" parameter which enables action/id matching
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        //It should look like this
        config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}"
            );
    

Also you can do it with custom attributes.

1 Comment

Currently, the entirety of my routing configuration is this: config.MapHttpAttributeRoutes();. I am not using template routing. To me it doesn't seem like this will solve my problem. Why would I want to rename the paramter? id is the name I want, I just want it to be settable from the query string only (not the URI). Am I missing something?
0

Actually, I was wrong about my original code. The query string parameter did not work with the route I specified. Instead, I could do this:

[Route("", Name = "GetEmployeeById")]
[HttpGet]
public IHttpActionResult GetEmployee(int id)
{
    ...
}

And this will do what I want. It must be getting the name id from the function's parameter list.

Unfortunately, this means I can't put a constraint on it anymore, but I guess I can just validate within the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.