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

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

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      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|0>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'.

原文地址:https://www.cnblogs.com/Godric/p/10111594.html

时间: 2024-08-28 19:10:14

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

将DataTable转换为List&lt;T&gt;对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。

可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm 但是该方法在DataTable里某个字段类型是Int32会有问题,报异常:类型"System.Int64"的对象无法转换为类型"System.Int32". 可在赋值的时候加一句: if(pi.GetMethod.ReturnParameter.ParameterType.Name == "Int32&q

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

最近园里的TeamOne写了一篇<[C#] int与System.Int32有什么区别>,发现里面有不少精彩的评论,所以忍不住想这篇文章总结一下:> 本文的主要参考资料: 1.<理解C#中的System.Int32和int:并非鸡和鸡蛋>@Author:Dixin 2.<[C#] int与System.Int32有什么区别>@Author:TeamOne 一.问题的来源 MSDN说,int只不过是System.Int32的别名而已,也就是说: int i=1; S

Type mismatch: cannot convert from Enumeration&lt;String&gt; to Enumeration&lt;Object&gt;

完整错误信息: Description Resource Path Location TypeType mismatch: cannot convert from Enumeration<String> to Enumeration<Object> ResponseHeaderFilter.java /dwz-java/src/dwz/common/util line 46 Java Problem 解决办法: Type mismatch: cannot convert from

ora-01653: unable to extend table sys.aud$ by 8192 in tablespac system

ora-01653: unable to extend table sys.aud$ by 8192 in tablespac system[转载]. 在用sqlplus user/[email protected]登录数据库时报如下错误: ORA-00604: error occurred at recursive SQL level 1 ORA-01653: unable to extend table SYS.AUD$ by 8192 in tablespace SYSTEM ORA-02

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

在Java使用Redis的过程中遇见了一个问题, redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at … Caused by: java.util.NoSuchElementException: Unable to validate object at … 这个问题是说拿不到Redis的链接,因为validate通不过.登录一个客户端,手动ping一下

MVC c# 没有为类型“System.Nullable`1[System.Int32]”和“System.Int32”定义二进制运算符 Equal。

Entity framework 运用过程中,条件查询时会出现  MVC c# 没有为类型“System.Nullable`1[System.Int32]”和“System.Int32”定义二进制运算符 Equal. 处理方法:int类型进行连接是, 非空和非零的前提下才进行Equal,所以尝试转换成int成功就可以处理掉这个异常. int UserId ;           if (int.TryParse(Request["UserId"], out UserId))      

(C# Debug)A first chance exception of type &#39;System.ArgumentException&#39; occurred in System.Data.dll

Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll”. 但是并没有直接throw错误.无法知道具体在哪一步发生了这个错误. 如果想知道具体的内容,需要enable 这个debug If you do want to know, in Visual Studio -> Debug (main menu) ->

Object::connect: Cannot queue arguments of type &#39;QMap&lt;QString,QString&gt;&#39;(要使用qRegisterMetaType&lt;StringMap&gt;进行注册)

QObject::connect: Cannot queue arguments of type 'QMap<QString,QString>',(Make sure 'QMap<QString,QString>' is registered using qRegisterMetaType().). 上述错误,只有在跨线程信号传递时才会出现.  因为QMap是QT可识别的基本类型,不需要再注册元对象系统中,在同一个线程中运行没有问题. 源码: Cpp代码   // 线程类 thre

Object类、日期时间类、system类及StringBuilder字符串容器

一.Object类常用API 1.1 概述 java.lang.Object类是Java语言中的根类,即所有类的父类.Object类中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类就是Object. 如果一个类没有特别指定父类, 那么默认则继承自Object类. 1.2 toString方法 public String toString() //返回值为 对象的类型[email protected]+内存地址值 由于toString方法返回的结果是内存地址,而在开发中,经常需要按