Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove ErrorViewModel when using dotnet core with MVC

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!

like image 274
O Wood Avatar asked Jan 22 '19 16:01

O Wood


People also ask

How does MVC handle application error in global ASAX?

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.

What is exception filter in ASP.NET Core?

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.

How do you handle errors in MVC?

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.

Where can I see handle errors in ASP NET Core web APIs?

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.

What is DotNet-core-uninstall?

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

How to add using directive to errorviewmodel class?

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.


2 Answers

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.

like image 143
Majid Avatar answered Oct 20 '22 10:10

Majid


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.

like image 31
TanvirArjel Avatar answered Oct 20 '22 12:10

TanvirArjel