2

How can I get Post parameters and their values sent by the client within an action in MVC? I don't want to add a parameter to the action method matching the JSON structure sent by the client. I want to use the Request or any other property that exposes this information.

For instance, if the action is:

public ActionResult Index()

I don't want to write something like this to achieve the result:

public ActionResult Index(MyObject object)

Where MyObject is the class that matches the JSON sent by the client I want to navigate the Request or the raw text sent in the Request to get this info

Important: I'm executing via AJAX a HttpPost Request, I'm not submiting a form.

Using Request.InputStream works for submit Posts, but not for Ajax. The code for this is:

var input = new StreamReader(Request.InputStream).ReadToEnd();

In the case of Ajax the InputStream is empty

4 Answers 4

3

I was in the right track but something was missing. This code works

Request.InputStream.Position = 0;
var input = new StreamReader(Request.InputStream).ReadToEnd();
Sign up to request clarification or add additional context in comments.

Comments

2

You still have access to the Request.Form object in an MVC action.

Request.Form["name"]

1 Comment

This is not what I'm looking for. Currently I'm executing via AJAX a HttpPost Request, I'm not submiting a form
2

I'm a little late to the party, but I will offer an alternative that will allow you to access Request.Form with an Ajax post/form. This was tested in MVC 4 and jQuery 1.9.1.

If the controller's Request.Form is not populating for your Ajax post, it is likely because you are manually serializing and sending the data to the controller with a contentType of application/json (a common scenario).

Here is an jQuery.ajax example that will NOT populate Request.Form on the controller.

    // JSON serialized form data.
    var data = {"id" : "1234", "name" : "Dave"};

    $.ajax({
        type: "POST",
        url: serviceUrl,
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify(data || {}),
        success: function () { }
    });

Changing the contentType will cause the controller to process the post like a form submission.

    // Form encoded data.  See jQuery.serialize().
    var data = "id=1234&name=Dave";

    $.ajax({
        type: "POST",
        url: serviceUrl,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: "json",
        data: data,
        success: function () { }
    });

You can also leave contentType undefined as application/x-www-form-urlencoded; charset=UTF-8 it is the jQuery default.

I would note that I only used $.ajax to better illustrate what is going on. You could also use $.post, though you will still need to send form encoded data

Comments

1

You can use Request.InputStream to access the post data directly. See here:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

However, it does not make much sense to use MVC if your main concern is skipping the "Model" part of it. Hope this helps though.

4 Comments

This is really good, almost what I need. It worked for a regular page submit. You can see the code in the main article. Although it doesn't work for Ajax calls
Requests are requests, be is Ajax or not. I think the reason it's not working for you because your ajax post is in json, as json is not natievely understood by .Net... Are you parsing your json correctly? Using Json.net could make things easier.
I don't have access to the raw JSON. Where do I find it?
- Get the Request body from Request.InputStream - Read the input stream and convert it to string - Use Json.Net to deserialize the json object - At which time, you may wish to build a strongly typed model, or not (as you mentioned above)

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.