Use Razor without Microsoft.NET.Sdk.Web
Asked Answered
M

2

7

I am writing simple consoleApp (netcoreapp2.0)

<Project Sdk="Microsoft.NET.Sdk">

and want to run webserver with mvc.

class Program
{
    static void Main(string[] args)
    {
        WebHost.CreateDefaultBuilder()
            .ConfigureServices(services => services.AddMvc())
            .Configure(app => app.UseDeveloperExceptionPage().UseMvcWithDefaultRoute())
            .UseHttpSys().Build().Run();
    }
}

public class HomeController : Controller
{
    [HttpGet] public ActionResult Index() => View("Index");
}

I get an error while GET http//localhost:5000

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.

Probably reason is in Razor Engine. How can i make it work? What did i miss?

Mok answered 21/9, 2017 at 11:30 Comment(3)
sorry, <PreserveCompilationContext>true</PreserveCompilationContext> is the key May be delete this question is best choiceMok
Can you post your Index.cshtml view file?Peridium
I had similar problem and after VS update to 15.4.1 I can't open the project. I'm not author of the source, I got it to modify it but it works fine in coworker's computer.Naphthalene
P
2

That error message can be caused by a missing @using in your Index.cshtml view file. Try bypassing the index view and just return a string from your controller like this to see if the error message goes away.

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            WebHost.CreateDefaultBuilder()
                .ConfigureServices(services => services.AddMvc())
                .Configure(app => app.UseDeveloperExceptionPage().UseMvcWithDefaultRoute())
                .UseHttpSys().Build().Run();
        }
    }

    public class HomeController : Controller
    {
        [HttpGet] public string Index() => "Hello World!";
    }
}

csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.HttpSys" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
  </ItemGroup>

</Project>
Peridium answered 18/10, 2017 at 1:2 Comment(2)
"That error message can be caused by a missing @using in your Index.cshtml view file". That was it for me. VS didn't indicate that error.Needlework
I didn't see any errors when I bypassed the view. In setting a break point on the model that controller returns you should be able to inspect the type. Make sure the @model in the view uses that type and you should be good to go.Stoss
L
0

Happened to me when a view was referencing a model that didn't exist anymore. In my case it was a confirmation page, so not the "big" view that I had been working with. If you open up a lot of your views in VS the errors might become apparent.

Lowndes answered 1/4, 2018 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.