3

In a Web Apì project, I would like to use something like:

POST/mycontroller/1

but also POST/mycontroller/1?user=john

It is easy with GET because the framework routes correctly to each function. However, when I use POST, it does not work. I have 2 POST functions in the same controller. For example:

void Post(int id, string content)

and

void Post(int id, string content, string user)

I would hope when I call POST/mycontroller/1?user=john, the framework routes to Post(int id, string content, string user)

I know that I can use binding models, doing a model class and one unique POST function, but it is a mess because I have many functions and I would like to be able to use the query parameters to route the correct function. Is it possible?

1 Answer 1

9

Try declaring parameter with [FromBody] and [FromUri] attribute like this:

    public string Post(int id, [FromBody]string content, [FromUri] string user)
    {
        return "content = " + content + "user = " + user;
    }

With above code I was able to call

/Test/1?user=Ryan

Request body

"Test Body"

and the result is:

"content = Test Bodyuser = Ryan"

Hope this helps.

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.