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!
Perhaps a better way of handling errors in MVC is to apply the HandleError attribute to your controller or action and update the Shared/Error. aspx file to do what you want. The Model object on that page includes an Exception property as well as ControllerName and ActionName.
Exception filters apply global policies to unhandled exceptions that occur before the response body has been written to. Result filters: Run immediately before and after the execution of action results. Run only when the action method executes successfully.
For an MVC app, the project template includes an Erroraction method and an Error view for the Home controller. The exception handling middleware re-executes the request using the originalHTTP method. If an error handler endpoint is restricted to a specific set of HTTP methods, it runs only for those HTTP methods.
See Handle errors in ASP.NET Core web APIsfor web APIs. Developer exception page The Developer Exception Pagedisplays detailed information about unhandled request exceptions. ASP.NET Core apps enable the developer exception page by default when running in the Development environment.
The.NET Uninstall Tool (dotnet-core-uninstall) lets you remove.NET SDKs and runtimes from a system. A collection of options is available to specify which versions should be uninstalled. Visual Studio dependency on.NET Core SDK versions
Copy the namespace name of the ErrorViewModel class. Add a new using directive just couple of lines above of the line having the error you reported and paste the copied namespace value. The newly added using directive will looks like this - using [something].Models Now the build should be successful.
I had the same problem and the following solution worked for me:
obj
and bin
directoriesNow it should work as a normal WebApi project.
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:
Error
method from HomeController
Error.cshtml
file from Views/Shared
folder_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With