Question

I have a Controller action like this:

public async Task<HttpResponseMessage> Put(
        [FromUri] ResourceSpec resource,
        //ValidationType? validationType,
        CreateResourceRequest body)

public enum ValidationType
{
    name = 1,
}

It works great - the resource object and body object (classes) are populated from the URL routing parameters and the request body respectively.

What I want to do that I'm having trouble with is bind the optional validationType parameter to a query string parameter in the PUT request.

When I uncomment the ValidationType line (and validation type is an Enum), and send an url with a ?validationType=name query parameter, I get errors routing to an action, e.g. 405 method not allowed (Allow: GET) (And yes, I have a GET method on this controller, which doesn't take a validationType parameter.)

And yes, I've tried setting the parameter as optional in the routing code:

defaults: new { controller = "Resource", validationType = RouteParameter.Optional }

and tried the

[FromUri] ValidationType? validationType,

variation. Please tell me why this is not working!

Update: OK I got it to route but not to parameter bind. If I introduce a new 'none' enum member and do

defaults: new { controller = "Resource", validationType = ValidationType.none }

Then my action will get called. But it will always get parameter value ValidationType.none even if my query string has ?validating=name! [FromUri] still makes no difference. What gives?

Was it helpful?

Solution

Duh... I had the name of the parameter wrong actually. I confused 'validating' and 'validationType'. How embarrassing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top