2016-11-11 30 views
0

點網CORE我想補充MVC視圖頁面中MVC的核心Web API soultion。我已經添加了控制器和視圖,但只要我嘗試訪問它,它就會給出運行時錯誤,如下所示。ASPNET核心MVC視圖頁面沒有加載屬性格式

創建項目時,如果我選擇Web應用程序一切正常。但我有一個現有的休息API項目,這讓我創建了兩個項目。我認爲我們可以擴展相同的項目,以便根據需要託管Rest API以及網頁。我們可以嗎?

I am continuously getting this run time error

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

cshtml文件中的代碼是什麼? – thoean

+0

Hello world!

多數民衆贊成在所有 – user2449952

+0

請更新您的答案與您的project.json,Startup.cs,您的MVC控制器和您的視圖。 –

回答

1

看來我們需要preserveCompilationContext在project.json設置爲true。對於Razor視圖的運行時編譯是必要的。

​​