How to remove ErrorViewModel when using dotnet core with MVC

隐身守侯 提交于 2021-01-27 04:37:07

问题


If ErrorViewModel is removed from my project, it won't run, failing at app.UseMvc() with the error:

System.TypeLoadException: 'Could not load type 'DocumentGenerationService.Models.ErrorViewModel' from assembly 'DocumentGenerationService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'

It is a dotnet core 2.2 application with mvc. I've removed everything that is unnecessary to the application, which includes all the views, as this is a webapi project.

Why can't I remove ErrorViewModel from the project? It's making me sad.

For reference, here's how the startup class looks:

public class Startup
    {
        ...

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddSignalR();

            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            ...
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
           ...

            app.UseMvc();
        }
    }

Any ideas how to get rid of the ErrorViewModel class and have the project run? Or an explanation of why it isn't possible?

Thanks StackOverflowers!


回答1:


I had the same problem and the following solution worked for me:

  1. Remove all the ErrorViewModel related files like Controllers, Views, etc...
  2. Close the Visual Studio if it's open
  3. Go to the source directory and remove obj and bin directories
  4. Open the project with Visual Studio and run it

Now it should work as a normal WebApi project.




回答2:


First welcome to the Stack Overflow community.

Now after removing the ErrorViewModel from models folder you need to do following things to remove the ErrorViewModel related codes:

  • Delete the Error method from HomeController
  • Delete Error.cshtml file from Views/Shared folder
  • Now check _ViewImports file in the Views folder. If there is any red line starting with @using then please remove this line.

Now build the solution and run again. Hope it will run now gracefully.



来源:https://stackoverflow.com/questions/54312357/how-to-remove-errorviewmodel-when-using-dotnet-core-with-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!