0

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?

2
  • Can't verify right now but I think you also need the FromUri attribute on ValidationType. The default for PUT is to bind to the request body not the URI. Only GET binds to URI because there is no body. The query string is part of tje uri. Commented Jan 18, 2014 at 16:59
  • I've tried using [FromUri] and it makes no difference. I've also experimented with GET and it seems like it has the same problem as PUT. Commented Jan 18, 2014 at 17:08

1 Answer 1

2

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

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

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.