微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在 razor 中创建编辑器,需要 System.Windows.Forms

如何解决无法在 razor 中创建编辑器,需要 System.Windows.Forms

我在 ASP.NET 中有这个非常基本的表单:

@using (Html.BeginForm("Edit","Home",FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <fieldset>
        @Html.EditorFor(item => item.Name)
        <input type="submit" value="Upload" />
    </fieldset>
}

当我删除@Html.EditorFor(item => item.Name) 时,一切正常。但是当我添加它时,我收到以下错误

FileNotFoundException:无法加载文件或程序集“System.Windows.Forms,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”。

为什么我需要 ASP 中的 Windows 窗体???有没有其他人遇到过同样的问题?我在 Rider 中使用 MVC 创建了一个普通的 WebApplication。目标框架为 netcoreapp3.1,语言版本为 C# 8.0。如果您需要更多详细信息,请随时询问。

错误的整个调用栈如下:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'. File not found.
File name: 'System.Windows.Forms,PublicKeyToken=b77a5c561934e089'
   at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule,RuntimeType type,IRuntimeMethodInfo pCtor,Byte** ppBlob,Byte* pEndBlob,Int32* pcNamedArgs)
   at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module,IRuntimeMethodInfo ctor,IntPtr& blob,IntPtr blobEnd,Int32& namedArgs)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes,RuntimeModule decoratedModule,Int32 decoratedMetadataToken,RuntimeType attributeFilterType,Boolean mustBeInheritable,ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule,Int32 pcaCount,RuntimeType attributeFilterType)
   at System.Reflection.RuntimePropertyInfo.GetCustomAttributes(Type attributeType,Boolean inherit)
   at System.Attribute.InternalGetCustomAttributes(PropertyInfo element,Type type,Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element,Boolean inherit)
   at System.Reflection.CustomAttributeExtensions.GetCustomAttributes(MemberInfo element)
   at Microsoft.AspNetCore.Mvc.ModelBinding.modelattributes.GetAttributesForProperty(Type containerType,PropertyInfo property,Type modelType)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.CreateSinglePropertyDetails(ModelMetadataIdentity propertyKey,PropertyHelper propertyHelper)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.CreatePropertyDetails(ModelMetadataIdentity key)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider.GetMetadataForProperties(Type modelType)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Properties()
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ExpressionMetadataProvider.FromLambdaExpression[TModel,TResult](Expression`1 expression,ViewDataDictionary`1 viewData,IModelMetadataProvider MetadataProvider)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpressionProvider.CreateModelExpression[TModel,TValue](ViewDataDictionary`1 viewData,Expression`1 expression)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper`1.GetModelExpression[TResult](Expression`1 expression)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper`1.EditorFor[TResult](Expression`1 expression,String templateName,String htmlFieldName,Object additionalViewData)
   at Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperEditorExtensions.EditorFor[TModel,TResult](IHtmlHelper`1 htmlHelper,Expression`1 expression)
   at AspNetCore.Views_Home_Edit.ExecuteAsync() in /my/path/Edit.cshtml:line 16
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page,ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page,ViewContext context,Boolean invokeViewStarts)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext,String contentType,Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext,Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext,IView view,ViewDataDictionary viewData,ITempDataDictionary tempData,Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context,ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterasync>g__Awaited|29_0[TFilter,TFilterasync](ResourceInvoker invoker,Task lastTask,State next,Scope scope,Object state,Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterasync](State& next,Scope& scope,Object& state,Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from prevIoUs location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker,Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next,Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from prevIoUs location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker,Task task,Idisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint,Task requestTask,ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

解决方法

我发现了问题!我从另一个项目的文件中导入了一个枚举,其中 System.Windows.Forms 也被导入。将枚举移到别处解决了这个问题。现在我只导入不导入 System.Windows.Forms 的东西。

如果您碰巧遇到同样的问题:请检查您的导入,以及您的导入的导入。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。