c# - [Serializable] 类的 Web.Api 模型绑定(bind)不起作用

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

<分区>

我有一个类;

[Serializable] 
public class ApiClass
{        
    public string UserId { get; set; }
    public DateTime CreateTime { get; set; }        
}

在我的 web api Controller 中方法是这样的;

    public Guid Post(ApiClass apiClass)
    {
         // do some stuff with parameter.
    }

当我将对象作为 json 请求发送时,出于某种原因,如果我为我的类使用 [Serializable] 属性,它不会解析。但如果我删除它,我可以从参数中获取值。

会是什么原因呢?

最佳答案

无论 Serializable 属性如何,您的模型绑定(bind)都应该可以正常工作,确保发送数据的代码是正确的。下面的代码在 DTO 上使用和不使用 Serializable 属性时都可以正常工作。

$(function() {

    var data = { userId : "Shyju" };

    $.post("api/Values", data, function(response) {
        document.write(response);
    });

});

和值 Controller 中的服务器代码

public string Post(ApiClass model)
{
    return "Received" + model.UserId;
}

关于c# - [Serializable] 类的 Web.Api 模型绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393573/

相关文章:

c# - HttpContext.Current.Response.Filter : Response filter is not valid

c# - Web API 过滤器未捕获方法组引发的异常

asp.net - 在 ASP.NET Core 中检测移动设备

asp.net-web-api2 - 操作参数的必需属性不起作用

asp.net-mvc - 为什么在 MVC 4 中执行 POST 会引发异常?

c# - 如何在字符串中添加特殊字符

c# - MonoTouch - 当数据源被清除时如何立即重绘UITableView

c# - 如何为应用程序设置样式,以便所有控件均显示在应用程序的配色方案中

.net - 使用 ASP.NET MVC 可以在一个页面上有两个表单吗?

c# - Kendo DataSourceRequest 和 DataSourceResult 与 .Net Core