简体   繁体   English

如果Model具有Int64,则放置/发布json不能与ODataController一起使用

[英]Put/Post json not working with ODataController if Model has Int64

I have this Data Object with an Int64 column: 我有一个Int64列的数据对象:

[TableAttribute(Name="dbo.vw_RelationLineOfBusiness")]
[DataServiceKey("ProviderRelationLobId")] 
public partial class RelationLineOfBusiness
{

    #region Column Mappings
    private System.Guid _Lineofbusiness;
    private System.String _ContractNumber;
    private System.Nullable<System.Int32> _ProviderType;
    private System.String _InsuredProviderType;
    private System.Guid _ProviderRelationLobId;
    private System.String _LineOfBusinessDesc;
    private System.String _CultureCode;
    private System.String _ContractDesc;
    private System.Nullable<System.Guid> _ProviderRelationKey;
    private System.String _ProviderRelationNbr;
    **private System.Int64 _AssignedNbr;**

When I post/Put object through my OData controller using HttpClient and NewtsonSoft: 当我使用HttpClient和NewtsonSoft通过我的OData控制器发布/放置对象时:

partial class RelationLineOfBusinessController : ODataController { partial class RelationLineOfBusinessController:ODataController {

public HttpResponseMessage PutRelationLineOfBusiness([FromODataUri] System.Guid key, Invidasys.VidaPro.Model.RelationLineOfBusiness entity) public HttpResponseMessage PutRelationLineOfBusiness([FromODataUri] System.Guid key,Invidasys.VidaPro.Model.RelationLineOfBusiness entity)

the entity object is null and the error in my modelstate : 实体对象为null并且我的模型状态中存在错误:

"Cannot convert a primitive value to the expected type 'Edm.Int64'. See the inner exception for more details." “无法将原始值转换为预期类型'Edm.Int64'。有关更多详细信息,请参阅内部异常。”

I noticed when I do a get on my object using the below URL: 我注意到当我使用以下网址获取对象时:

Invidasys.Rest.Service/VidaPro/RelationLineOfBusiness(guid'c6824edc-23b4-4f76-a777-108d482c0fee') Invidasys.Rest.Service / VidaPro / RelationLineOfBusiness(guid'c6824edc-23b4-4f76-a777-108d482c0fee')

my json looks like the following - I noticed that the AssignedNbr is treated as a string. 我的json看起来如下 - 我注意到AssignedNbr被视为一个字符串。

{ "odata.metadata":"Invidasys.Rest.Service/VIDAPro/$metadata#RelationLineOfBusiness/@Element", "Lineofbusiness":"ba129c95-c5bb-4e40-993e-c28ca86fffe4","ContractNumber":null,"ProviderType":null, "InsuredProviderType":"PCP","ProviderRelationLobId":"c6824edc-23b4-4f76-a777-108d482c0fee", "LineOfBusinessDesc":"MEDICAID","CultureCode":"en-US","ContractDesc":null, "ProviderRelationKey":"a2d3b61f-3d76-46f4-9887-f2b0c8966914","ProviderRelationNbr":"4565454645", "AssignedNbr":"1000000045" ,"Ispar":true,"ProviderTypeDesc":null,"InsuredProviderTypeDesc":"Primary Care Physician", "StartDate":"2012-01-01T00:00:00","EndDate":"2014-01-01T00:00:00","Created":"2014-06-13T10:59:33.567", "CreatedBy":"Michael","Updated":"2014-06-13T10:59:33.567","UpdatedBy":"Michael" } {“odata.metadata”:“Invidasys.Rest.Service/VIDAPro/$metadata#RelationLineOfBusiness/@Element”,“Lineofbusiness”:“ba129c95-c5bb-4e40-993e-c28ca86fffe4”,“ContractNumber”:null,“ProviderType” :null,“InsuredProviderType”:“PCP”,“ProviderRelationLobId”:“c6824edc-23b4-4f76-a777-108d482c0fee”,“LineOfBusinessDesc”:“MEDICAID”,“CultureCode”:“en-US”,“ContractDesc”:null ,“ProviderRelationKey”:“a2d3b61f-3d76-46f4-9887-f2b0c8966914”,“ProviderRelationNbr”:“4565454645”, “AssignedNbr”:“1000000045” ,“Ispar”:true,“ProviderTypeDesc”:null,“InsuredProviderTypeDesc”:“初级保健医生“,”StartDate“:”2012-01-01T00:00:00“,”EndDate“:”2014-01-01T00:00:00“,”创建“:”2014-06-13T10:59: 33.567“,”CreatedBy“:”Michael“,”Updated“:”2014-06-13T10:59:33.567“,”UpdatedBy“:”Michael“}

When I do a PUT with httpclient the JSON is showing up in my restful services as the following and the json for the AssignedNbr column is not in quotes which results in the restful services failing to build the JSON back to an object. 当我使用httpclient进行PUT时,JSON出现在我的restful服务中,如下所示,AssignedNbr列的json不在引号中,导致restful服务无法将JSON构建回对象。 I played with the JSON and put the AssignedNbr in quotes and the request goes through correctly. 我使用JSON并将AssignedNbr放在引号中,请求正确完成。

{ "AssignedNbr":1000000045 ,"ContractDesc":null,"ContractNumber":null,"Created":"/Date(1402682373567-0700)/", "CreatedBy":"Michael","CultureCode":"en-US","EndDate":"/Date(1388559600000-0700)/","InsuredProviderType":"PCP", "InsuredProviderTypeDesc":"Primary Care Physician","Ispar":true,"LineOfBusinessDesc":"MEDICAID", "Lineofbusiness":"ba129c95-c5bb-4e40-993e-c28ca86fffe4","ProviderRelationKey":"a2d3b61f-3d76-46f4-9887-f2b0c8966914", "ProviderRelationLobId":"c6824edc-23b4-4f76-a777-108d482c0fee","ProviderRelationNbr":"4565454645","ProviderType":null, "ProviderTypeDesc":null,"StartDate":"/Date(1325401200000-0700)/","Updated":"/Date(1408374995760-0700)/","UpdatedBy":"ED"} { “AssignedNbr”:1000000045 ,“ContractDesc”:null,“ContractNumber”:null,“Created”:“/ Date(1402682373567-0700)/”,“CreatedBy”:“Michael”,“CultureCode”:“en-US “,”EndDate“:”/ Date(1388559600000-0700)/“,”InsuredProviderType“:”PCP“,”InsuredProviderTypeDesc“:”Primary Care Physician“,”Ispar“:true,”LineOfBusinessDesc“:”MEDICAID“,” Lineofbusiness“:”ba129c95-c5bb-4e40-993e-c28ca86fffe4“,”ProviderRelationKey“:”a2d3b61f-3d76-46f4-9887-f2b0c8966914“,”ProviderRelationLobId“:”c6824edc-23b4-4f76-a777-108d482c0fee“,”ProviderRelationNbr“ :“4565454645”,“ProviderType”:null,“ProviderTypeDesc”:null,“StartDate”:“/ Date(1325401200000-0700)/”,“Updated”:“/ Date(1408374995760-0700)/”,“UpdatedBy” “ED”}

The reason we wanted to expose our business model as restful services was to hide any data validation and expose all our databases in format that is easy to develop against. 我们希望将业务模型公开为restful服务的原因是隐藏任何数据验证并以易于开发的格式公开我们所有的数据库。 I looked at the DataServiceContext to see if it would work and it does but it uses XML to communicate between the restful services and the client. 我查看了DataServiceContext以查看它是否可以正常工作,但它使用XML在restful服务和客户端之间进行通信。 Which would work but DataServiceContext does not give the level of messaging that HttpRequestMessage/HttpResponseMessage gives me for informing users on the errors/missing information with their post. 哪个会起作用,但DataServiceContext没有给出HttpRequestMessage / HttpResponseMessage给我的消息传递级别,用于通过帖子通知用户错误/缺失信息。

We are planning on supporting multiple devices from our restful services platform but that requires that I can use NewtonSoft Json as well as Microsoft's DataContractJsonSerializer if need be. 我们计划从我们的restful服务平台支持多个设备,但这需要我可以使用NewtonSoft Json以及Microsoft的DataContractJsonSerializer(如果需要)。

My question is for a restful service standpoint - is there a way I can configure/code the restful services to take in the AssignedNbr as in JSON as without the quotes. 我的问题是一个宁静的服务立场 - 有没有一种方法可以配置/编码宁静的服务,以便像JSON一样接受AssignedNbr,而不是引号。

Or from a JSON standpoint is their a way I can get the JSON built without getting into the serializing business nor do I want our clients to have deal with custom serializers if they want to write their own apps against our restful services. 或者从JSON的角度来看,他是一种可以在不进入序列化业务的情况下构建JSON的方式,如果他们想要针对我们的休息服务编写自己的应用程序,我也不希望我们的客户处理自定义序列化程序。

Any suggestions? 有什么建议么?

Thanks. 谢谢。

I think you can migrate to Web API 2.2 for OData V4. 我认为您可以迁移到适用于OData V4的Web API 2.2。 Here's the information: 这是信息:

Announcing the Release of ASP.NET MVC 5.2, Web API 2.2 and Web Pages 3.2 宣布发布ASP.NET MVC 5.2,Web API 2.2和Web Pages 3.2

OData V4 Spec says: OData V4 Spec说:

3.2 Controlling the Representation of Numbers 3.2控制数字的表示

The IEEE754Compatible=true format parameter indicates that the service MUST serialize Edm.Int64 and Edm.Decimal numbers (including the odata.count, if requested) as strings. IEEE754Compatible = true格式参数表示服务必须将Edm.Int64和Edm.Decimal数字(包括odata.count,如果请求)序列化为字符串。 If not specified, or specified as IEEE754Compatible=false, all numbers MUST be serialized as JSON numbers. 如果未指定或指定为IEEE754Compatible = false,则所有数字必须序列化为JSON数字。

This enables support for JavaScript numbers that are defined to be 64-bit binary format IEEE 754 values [ ECMAScript ] (see section 4.3.1.9) resulting in integers losing precision past 15 digits, and decimals losing precision due to the conversion from base 10 to base 2. OData JSON payloads that format Edm.Int64 and Edm.Decimal values as strings MUST specify this format parameter in the media type returned in the Content-Type header. 这样可以支持定义为64位二进制格式IEEE 754值[ ECMAScript ](参见第4.3.1.9节)的JavaScript数字,导致整数丢失精度超过15位,并且由于从基数10转换为十进制而导致精度丢失base 2.将Edm.Int64和Edm.Decimal值格式化为字符串的OData JSON有效负载必须在Content-Type标头中返回的媒体类型中指定此格式参数。

So, for payload as: 因此,对于有效载荷:

@"{ 
    ""Lineofbusiness"": ""ba129c95-c5bb-4e40-993e-c28ca86fffe4"",
    ""AssignedNbr"": ""1000000045""
  }";

you should set: 你应该设置:

request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;IEEE754Compatible=true");

Otherwise, you shouldn't. 否则,你不应该。

Sam Xu is exactly right and should be marked as the answer. Sam Xu完全正确,应该标记为答案。

However, I wanted to add exactly what you need to do to add this to the pipeline. 但是,我想准确添加您需要做的事情以将其添加到管道中。

First, you can set this global, per route etc. You can find that information here: http://www.asp.net/web-api/overview/advanced/http-message-handlers 首先,您可以设置此全局,每条路线等。您可以在此处找到该信息: http//www.asp.net/web-api/overview/advanced/http-message-handlers

Below you'll find an example that will work. 您将在下面找到一个可行的示例。

public static void Configuration(IAppBuilder builder)
    {
        HttpConfiguration config = new HttpConfiguration();       

        config.MessageHandlers.Add(new MethodOverrideHandler());
    }

public class MethodOverrideHandler : DelegatingHandler
{       
    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;IEEE754Compatible=true");

        return base.SendAsync(request, cancellationToken);
    }
}

Alternatively, try changing the type you send to your web api to Number instead of string 或者,尝试将发送到Web api的类型更改为Number而不是string

Also, check the Type of the decimal that you are sending. 另外,检查要发送的小数类型。 If it's type 'string' you can change it to type number. 如果是'string'类型,您可以将其更改为类型编号。 For my service, making this change no longer throws the error. 对于我的服务,进行此更改不再引发错误。

//Gnuget Package Manager Install-Package numeral  

if (typeof newValue === 'string') 
{
newValue = numeral().unformat(newValue);
}

  odatajs.oData.request(
             {
                 requestUri: xxx,
                 method: "PATCH",
                 data: { PriceNull: newValue }
             }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过JSON发送长整数(Int64)到WCF数据服务 - Sending a long (Int64) via JSON to a WCF Data Service go中int64的json解析; 空值 - json parsing of int64 in go; null values Python 错误:TypeErrpr:int64 类型的对象不是 JSON 可序列化的 - Python error: TypeErrpr: Object of type int64 is not JSON serializable Go 中的问题与 JSON 解组 map[string]int64 - Issues in Go with JSON unmarshal of map[string]int64 如何将json响应的最大值增加到int64 - How to increase maximum of json response upto int64 在 Go 中解析 json 时保留 int64 值 - preserve int64 values when parsing json in Go jsonpb,为什么要把int64解码成json,结果是字符串。 像 int64 str=10 --&gt;str:&quot;10&quot; - jsonpb, why decode int64 to json,the result is string. like int64 str=10 -->str:"10" 不同类型反射的Golang JSON数组:float64 vs int64 - Golang JSON array of different types reflection: float64 vs int64 Powershell Core 将 JSON 中的数字反序列化为 Int64,而 Windows Powershell 将其作为 Int32 - Powershell Core deserializes numbers in JSON as Int64 vs Windows Powershell which does it as Int32 类型错误:int64 类型的 Object 不是 JSON 可序列化 | pandas 聚合函数和 json.dumps() 错误 - TypeError: Object of type int64 is not JSON serializable | pandas aggregation functions and json.dumps() error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM