首页 文章

AspNet Core MVC 视图页面未加载属性

提问于
浏览
0

Dot NET CORE我试图在 MVC CORE web API soultion 中添加 MVC 视图 PAGE。我添加了控制器和视图,但是一旦我尝试访问它,它就会产生如下运行时错误。

如果我选择 Web 应用程序创建项目,一切正常.But 我有一个现有的 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>

1 回答

  • 1

    看来我们需要在 project.json 中将 preserveCompilationContext 设置为 true。这对于 Razor 视图的运行时编译是必要的。

    "buildOptions": {
            "emitEntryPoint": true,
            "debugType": "portable",
            "preserveCompilationContext": true
          },
    

相关问题