Reputation: 1767
I have created basic MVC application in asp.net core with Visual Studio 2017. I got an error when trying to render index.cshtml
An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.
Generated Code
One or more compilation references are missing. Possible causes include a missing 'preserveCompilationContext' property under 'buildOptions' in the application's project.json.
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
+ using System;
...
My whole console application is as short as it can be:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseMvc();
}
}
public class HomeController : Controller
{
[HttpGet("/")]
public ActionResult Index()
{
ViewBag.Message = "Hello world!";
ViewBag.Time = DateTime.Now;
return View();
}
}
}
Structure of my "project"
View
It's funny because the error tells me that I have to change something in project.json, but since Visual Studio 2017 there is no project.json anymore.
My view is in fact simple static html file and does not require any references to models or anything. But errors are complaining about missing types:
I tried to compare my project to web application template but I havent found what's causing razor views not to build in console application.
What am I missing here?
Upvotes: 3
Views: 1483
Reputation: 9346
There is a different SDK to use.
Console Apps use: Microsoft.NET.Sdk
Web Apps use: Microsoft.NET.Sdk.Web
This can be updated in your *.csproj
Upvotes: 0
Reputation: 64121
Try adding
<PropertyGroup>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
to your csproj. And to avoid such mistakes next time, please use the ASP.NET Core templates you can find when you create a new project. They have the necessary stuff and commonly used packages added to it's csproj.
Upvotes: 4