Search code examples
asp.net-coreasp.net-mvc-5asp.net-core-3.1

The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' .NET MVC 5 to .NET Core 3.1 MVC Migration


I have almost completed my migration from ASP.NET MVC 5 to .NET Core 3.1 and I am getting this error,

The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assemble reference?).

The error is occurring in my /Views/Shared/Error.cshtml view, where I am setting the model.

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error"
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

I have removed all of the AspNetCore packages and re-added them, and I have tried restarting my Visual Studio (VS19), all to no avail.

Perhaps the error handling has been moved out of the MVC package in .NET Core? That’s my best guess.


Solution

  • In ASP.NET Core 3.1, the main namespace for a MVC application is Microsoft.AspNetCore.Mvc. Try using that instead of System.Web.Mvc.

    Error Handling in ASP.NET Core

    That said, as far as error handling in particular is concerned, the approach has changed quite a bit from ASP.NET Framework, and the HandleErrorInfo class no longer exists. For more information on the new approach, see Handle errors in ASP.NET Core. Instead of using the HandleErrorInfo class to retrieve information on an exception, for instance, you now use an implementation of IExceptionHandlerPathFeature—though there's a bit more to it than that, as covered in the documentation.

    Migration Plan

    Ultimately, this is just the first of many differences you're likely to run into when migrating to ASP.NET Core. Given that, I'd recommend reading through Microsoft's Migrate from ASP.NET to ASP.NET Core guide, which provides a useful summary of what has changed, and how to approach the migration.

    Reference Project

    In addition, assuming you're using Visual Studio, I'd recommend creating a new ASP.NET Core Web Application project, which will create a basic ASP.NET Core 3.1 MVC application. This can be really useful to reference when performing a migration, as it establishes what a "text book" implementation should look like, and gives you an idea of what's changed and how to approach similar problems in the new framework.

    Final Thoughts

    Ultimately, a lot of the changes in ASP.NET Core are simply a matter of adding references to the correct NuGet packages, updating the namespaces, and, in a few cases, updating code to match changes in the class signatures. Still, there remain a number of areas where the old approaches (and the classes that supported them) no longer exist—as is the case here—and thus require additional thought and care.

    Positively, this is a lot easier with .NET Core 3.x than it was with previous versions, as Microsoft has done a ton to close the feature parity gap between .NET Framework and .NET Core over the last four years. And, once you've finished the migration, I think you'll find your new site much cleaner and easier to maintain.