【问题标题】:AspNet Core MVC view page not loading properyAsp Net Core MVC 视图页面未正确加载
【发布时间】:2016-11-11 05:33:30
【问题描述】:

Dot NET CORE 我正在尝试在 MVC CORE Web API 灵魂中添加 MVC 视图页面。我已经添加了控制器和视图,但是当我尝试访问它时,它会给出如下运行时错误。

如果我选择 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();
        }
    }

查看(索引.cshtml)

<h1>Hello world!</h1>

【问题讨论】:

  • cshtml文件中的代码是什么?
  • 你好世界!

    就是这样
  • 请用你的 project.json、Startup.cs、你的 MVC 控制器和你的视图更新你的答案。
  • 已在问题中更新。请看一下
  • 您是否使用 Nuget 之外的其他存储库作为您的软件包?例如,我找不到“OpenIddict”包。

标签: asp.net-mvc asp.net-core asp.net-core-mvc .net-core


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多