Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'Hosting' does not exist in the namespace 'Microsoft.AspNetCore.Razor'

I've created a project using the Visual Studio .NET Core 2.1 SDK + React Template.

I'm met with the following error when running the project:

One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.

The type or namespace name 'Hosting' does not exist in the namespace 'Microsoft.AspNetCore.Razor' (are you missing an assembly reference?)

enter image description here

I've done what research I could and have tried the following to no avail:

  • dotnet restore in Package Manager Console
  • Deleting my bin and obj folders
  • Opening _ViewImports.cshtml and adding a @using for every namespace in my solution

If I modify the HomeController Index() from returning the View() to instead [HttpGet] public string Index() => "Hello World!";, the text is returned without any error.


Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<div id="react-app">Loading...</div>

@section scripts {
    <script src="~/dist/main.js" asp-append-version="true"></script>
}

HomeController.cs

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace sample_project.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Error()
        {
            ViewData["RequestId"] = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
            return View();
        }
    }
}
like image 638
Tyler Roper Avatar asked Jun 12 '18 20:06

Tyler Roper


1 Answers

Shortly after posting this I've solved the issue.

I had upgraded my project to .NET Core 2.1 but one of the references (Microsoft.AspNetCore.All) was version 2.0.8.

Upon updating this to 2.1.0, the project now works as expected.

like image 120
Tyler Roper Avatar answered Sep 27 '22 19:09

Tyler Roper