0

I tried searching about how to post JSON data from ASP.NET MVC Controller, all in vain.

I have a post method in my Controller, which receives JSON data from the client, and does format validation, before it forwards it another location.

I am not able to figure out how to post data from this method to the other location.

Please Help. Thanks.

2 Answers 2

0

Here is the async code for it, so remember to await the method

private async Task DoStuff(string s)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri('whatever');
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.PostAsJsonAsync("api/whatever/",s);
    if (!response.IsSuccessStatusCode)
    {
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It seems unreasonable to round-trip this through the browser. If the action is on the same server, then I would probably abstract the processing code to a shared class and just invoke it after validation, returning the correct response directly from the validation action. If it's on a different server then I'd have the client-side code perform the validation check and, on success, simply POST to the processing action.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.