简体   繁体   中英

ASP.NET MVC - access parameters NOT posted via form



Current Scenario

I am using MVC 4, .net 4.5 on vs2012. I have an action which accepts a custom type. This custom type(model) is tightly bound to a view. I am making a POST via AJAX using JSON. Post will only post the relevant data and no form. Its content type is "application/json; charset=UTF-8". I am getting a nicely populated(read valid) model in my action.

The issue
Now I need to add a custom filter but I am unable to access the data via Request, Request.Form, Request.Param? I have been looking in System.Web.HttpContext.Current. If data is getting populated in my model, then it has to be somewhere in the request itself. I guess I am missing the finer print.

The javascript for posting data is somewhat like

$("#postData").click(function (event) {

    var savedObject = getJson(savedObject, parentContext);

    $.ajax({
        url: '/controller/action',
        contentType: 'application/json',
        dataType: 'json',
        data: savedObject,
        type: "POST",
        success: successCallBack,
        error: errorCallBack
    });
});

我不知道你的代码是什么但是。如果从你调用jQuery.ajax中删除contentType:“application / json; charset = utf-8”,将使用默认内容类型(form-urlencoded)和你拥有的json数据指定为您的数据参数(数据:{i:i,s:s,b:b})将正确映射到您的操作参数....所以,除非您真的想要发送json数据,只需删除contentType即可好的.....看看这个

I see your stated wish of sending JSON data only. If you really need to stick to this, you can access the raw parameters via Request.InputStream .

Controller:

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

This will get you a URL encoded string that you can manually parse.

Really, I would recommend Shahrooz's answer as the correct way to get where you want to go.

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.        
    var data = {"id" : "1234", "name" : "Dave"};
    data = $.param(data);

    $.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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM