最近在研究.netcore,尝试把前后端完全分离。但是在写接口的时候,Post参数是FromBody的时候报错了

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[]
An unhandled exception has occurred while executing the request.
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
at System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(Object value)
at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
at Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.DataAnnotationsModelValidator.Validate(ModelValidationContext validationContext)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.ValidateNode()
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitSimpleType()
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Validate(ModelMetadata metadata, String key, Object model, Boolean alwaysValidateAtTopLevel)
at Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, String prefix, Object model, ModelMetadata metadata)
at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.EnforceBindRequiredAndValidate(ObjectModelValidator baseObjectValidator, ActionContext actionContext, ParameterDescriptor parameter, ModelMetadata metadata, ModelBindingContext modelBindingContext, ModelBindingResult modelBindingResult)
at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

关键错误点Unable to cast object of type 'System.Int32' to type 'System.String'.提示是无法将“System.Int32”的对象强制转换为“System.String”。于是我就找了所有使用Int的地方,终于发现

   /// <summary>
/// ID
/// </summary>
[Key]
[StringLength(20)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override int ID { get; set; }

  在一个基类中有个Int类型的ID,不小心加上了StringLength属性。int类型是不能添加StringLength属性的,去掉之后就可以正常访问了。

Unable to cast object of type 'System.Int32' to type 'System.String'.的更多相关文章

  1. Unable to cast object of type 'System.Int32' to type 'System.Array'.

    x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...

  2. Unable to cast object of type 'System.String' to type 'System.Int32'.

    原因 数据库中 code 字段 类型为 varchar 而实体的类型为 int  导致string 类型无法转化为int 类型而报错 public int code { get; set; } 参考: ...

  3. 将DataTable转换为List<T>对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。

    可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm 但是该方法在DataTable里某个字段类型是 ...

  4. Jexus .Net at System.Net.Sockets.Socket.Connect (System.Net.IPAddress[] addresses, System.Int32 port)

    环境:Jexus(独立版)+MVC(5.2.3) +Redis+EF(6.0) Application Exception System.Net.Sockets.SocketException Con ...

  5. 《[C#] int与System.Int32有什么区别》

    最近园里的TeamOne写了一篇<[C#] int与System.Int32有什么区别>,发现里面有不少精彩的评论,所以忍不住想这篇文章总结一下:> 本文的主要参考资料: 1.< ...

  6. 解决:Redis:java.util.NoSuchElementException: Unable to validate object at

    在Java使用Redis的过程中遇见了一个问题, redis.clients.jedis.exceptions.JedisConnectionException: Could not get a re ...

  7. System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.

    08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...

  8. (C# Debug)A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll

    Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred ...

  9. 善待Redis里的数据--Unable to validate object

    又是一篇关于姿势的文章,为什么是”又”呢?因为上个星期刚写完一篇关于Apache Commons Pool的正确使用姿势的文章,点击此处阅读. Redis为我们提供便利的同时,我们也要善待里面的数据 ...

随机推荐

  1. 「黑科技」智能消毒防疫机器人 技术方案介绍-disinfection robot

    消毒机器人 小新防疫消杀机器人 - 自主导航全方位360°臭氧杀菌消毒机器人,采用臭氧无阻碍.无死角.遍布整个空间除菌:强力涡轮风机,30㎡室内空气循环6次/h,10分钟速效杀菌.除异味.自动转化为氧 ...

  2. 【转】centos7轻松搭建文件服务器

    1.安装apache服务器 yum install httpd 2:启动httpd服务 service httpd start 3:查看httpd服务器的版本 4:修改访问端口和文件路径,以防端口冲突 ...

  3. redis(七)---- SpringBoot和redis整合

    SpringBoot和Redis整合非常简单 添加pom依赖 <dependency> <groupId>org.springframework.boot</groupI ...

  4. wepy 小程序定时器(验证码倒计时) 数据绑定页面无刷新

    每次改变数据的时候记得调用  this.$apply() 验证码倒计时 使用的vant-weapp  UI组件 wxml: <van-col span="10" style= ...

  5. MBR&/BOOT&GRUB

      能正常工作的grub应该包 括一下文件:stage1.stage2.*stage1_5.menu.lst. 其中stage1要被安装(也就是写入)某个硬盘的主引导记录,或者某个活动分区(这个分区要 ...

  6. 使用Dom4j生成xml文件(utf-8编码)

    xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <result> <code> ...

  7. 在java中如何根据手机号查询号码归属地

    1.maven项目中配置 <dependency><groupId>com.googlecode.libphonenumber</groupId><artif ...

  8. jenkins job 杀进程不成功解决办法

    jenkins_job #!/bin/bash set -ex #打印执行过程 BUILD_ID=DONTKILLME #防止自杀 ssh dataexa@192.168.1.65 > /dev ...

  9. SQL基础教程(第2版)第8章 SQL高级处理:8-1 窗口函数

    第8章 SQL高级处理:8-1 窗口函数 ● 窗口函数可以进行排序.生成序列号等一般的聚合函数无法实现的高级操作.● 理解PARTITION BY和ORDER BY这两个关键字的含义十分重要. ■什么 ...

  10. 斐波那契数列 yield 和list 生成

    def fab_demo4(max): a,n,b = 0,0,1 while n < max: yield b # 生成器走到这一步返回b,需要再次调用才能继续执行 a,b = b,a+b n ...