Dot NET CORE 我试图在MVC CORE web API灵魂中添加MVC视图PAGE。我添加了控制器和视图,但是一旦我尝试访问它,它就会产生如下运行时错误。
如果我选择Web应用程序创建项目,一切正常。但是我有一个现有的rest API项目,这使我创建了两个项目。我认为我们可以扩展相同的项目来托管Rest API以及网页(如果需要)。我们不能吗?
Startup.cs
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
app.UseDeveloperExceptionPage();
app.UseCors(options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
app.UseStaticFiles();
app.UseOAuthValidation();
app.UseOpenIddict();
app.UseMvcWithDefaultRoute();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
services.AddAutoMapper();
services.AddDbContext<ApplicationDbContext>(options =>
services.AddIdentity<ApplicationUser, IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationDbContext, Guid>()
.AddDefaultTokenProviders();
services.AddOpenIddict<ApplicationUser, IdentityRole<Guid>, ApplicationDbContext, Guid>()
.AllowAuthorizationCodeFlow()
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.DisableHttpsRequirement()
.AddEphemeralSigningKey();
services.AddTransient<IEmailSender, AuthMessageSender>();
}
Project.json
{
"buildOptions": {
"emitEntryPoint": true,
"debugType": "portable"
},
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"OpenIddict": "1.0.0-alpha2-0448",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
"AspNet.Security.OAuth.Validation": "1.0.0-alpha2-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.AspNetCore.Cors": "1.0.0",
"Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.0.0",
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
"AutoMapper": "5.1.1",
"AutoMapper.Extensions.Microsoft.DependencyInjection": "1.1.2",
"Microsoft.DotNet.ProjectModel": "1.0.0-rc3-003121",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
"datalayer": "1.0.0.0",
"Common": "1.0.0-*"
},
"frameworks": {
"netcoreapp1.0": { }
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview2-final"
},
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final"
}
},
"scripts": {
"prepublish": [ "bower install" ],
"postpublish": "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
},
"publishOptions": {
"include": [ "wwwroot" ],
"includeFiles": [ "appsettings.json" ]
}
}
控制器
public class ActivationController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
}
查看(Index.cshtml)
<h1>Hello world!</h1>
答案 0 :(得分:1)
似乎我们需要在project.json中将preserveCompilationContext设置为true。这是运行时编译Razor视图所必需的。
"buildOptions": {
"emitEntryPoint": true,
"debugType": "portable",
"preserveCompilationContext": true
},