问题
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:
- Remove all the ErrorViewModel related files like Controllers, Views, etc...
- Close the Visual Studio if it's open
- Go to the source directory and remove
obj
andbin
directories - 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 fromHomeController
- Delete
Error.cshtml
file fromViews/Shared
folder - Now check
_ViewImports
file in theViews
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