c# - Web API 发布数据 : object reference not set to an instance of an object

标签 c# asp.net-web-api asp.net-web-api2

我正在尝试让 Web API 与 POST 一起工作,并且我的 Controller 中有以下内容:

public class mainGridController : ApiController
    {

        public class formData
        {
            public string module { get; set; }
            public string group { get; set; }
            public string staff { get; set; }
        }

        public HttpResponseMessage Post([FromBody] formData data)
        {
            string string_group = "";
            string string_staff = "";

            if (data.group != null)
            {

但我似乎在“if”语句中为 data.group 获取“对象引用未设置为对象的实例”。

这是我的路线信息:

protected void Application_Start(object sender, EventArgs e)
        {
             GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{data}",
            defaults: new { module = System.Web.Http.RouteParameter.Optional, data = System.Web.Http.RouteParameter.Optional}
            );
        }

有谁知道这可能是什么原因造成的?我正在尝试使用 jQuery 发帖。

这是我的 jQuery 代码:

var url = "api/mainGrid";
var source = {
    datatype: 'json',
    contentType: 'application/json; charset=utf-8',
    url: url,
    processData: false,
    type: "POST",
    id: "SEQUENCE",
    root: 'rowsinfo',
    cache: false,
    columns: [],
    datafields: [],
    beforeprocessing: function (data) {
        var columnsdata = new Array();
        var datafieldsdata = new Array();
        for (k in data.columnsinfo) {
            var col = {};
            col.text = data.columnsinfo[k]["DISPLAYNAME"];
            col.datafield = data.columnsinfo[k]["DISPLAYNAME"];
            var datafields = {};
            datafields.name = data.columnsinfo[k]["DISPLAYNAME"];
            columnsdata.push(col);
            datafieldsdata.push(datafields);
            source.columns = columnsdata;
            source.datafields = datafieldsdata;
        }
        $("#jqxgrid").jqxGrid({ columns: source.columns });
    },
    data: {
        group: JSON.stringify(checkedGroups),
        staff: JSON.stringify(checkedStaff),
        module: selectedModuleSEQ
    }
};

谢谢

最佳答案

问题是您已将 dataType 参数指定为 json 但您传递的是 JS object - 这当然不是 JSON .

您需要将您的 data 对象转换为实际的 JSON 才能工作

data: JSON.stringify({
    group: JSON.stringify(checkedGroups),
    staff: JSON.stringify(checkedStaff),
    module: JSON.stringify(selectedModuleSEQ)
}),

关于c# - Web API 发布数据 : object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753031/

相关文章:

c# - 如何在 ASP.NET 5 中使用基于 IAppBuilder 的 Owin 中间件

c# - 反序列化由 SignalR 作为抽象类传递的对象

asp.net-web-api - 具有Web API AuthorizeAttribute角色的Azure AD OAuth客户端凭据授予流

asp.net-mvc - 注册 IExceptionLogger OWIN 与 WebApiConfig

c# - 如何将字符串属性绑定(bind)到 ToolStripTextBox?

c# - 单元测试调用 wcf 服务的方法

asp.net-mvc - 将ID类型从字符串更改为int时,如何获取Web API中当前登录用户的ID?

asp.net - 返回项、Json(item) 和 Ok(item) 之间的 IHttpActionResult 区别

c# - AllowAnonymous 与 OverrideAuthorizeAttribute

c# - jquery 列表框返回用户选择的内容