简体   繁体   English

使用AddMvcCore()而不是AddMvc()的ASP.NET Core 2.0 Razor页面

[英]ASP.NET Core 2.0 Razor Pages using AddMvcCore() instead of AddMvc()

I'm not a big fan of using the pre-bundled AddMvc() and prefer to use the AddMvcCore() instead. 我不是使用预先捆绑的AddMvc()AddMvcCore()而是更喜欢使用AddMvcCore()

Having said that, I was wondering how to go about using the new (as of 2.0) AddRazorPages() with AddMvcCore() . 话虽如此,我想知道如何将新的(自2.0版本开始)的AddRazorPages()AddMvcCore()

For example, if we do a "bare-bones" configuration of middleware to only use AddRazorPages() which is found from the official repository 例如,如果我们对中间件进行“裸露”配置,以仅使用从官方存储库中找到的AddRazorPages()

// loaded the NuGet package Microsoft.AspNetCore.Mvc.RazorPages
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
        .AddRazorPages();
}

After I created a foo.cshtml page and placed it into the .\\Pages\\ directory, it is returning a 404 (Page not found) when I navigate to the URL \\Foo . 创建foo.cshtml页面并将其放入.\\Pages\\目录后,当我导航至URL \\Foo时,它将返回404(找不到页面)。

.\\Pages\\Foo.cshtml \\网页\\ Foo.cshtml

@page
@model IndexModel
@using Microsoft.AspNetCore.Mvc.RazorPages

@functions {
    public class IndexModel : PageModel
    {
        public string Message { get; private set; } = "In page model: ";

        public void OnGet()
        {
            Message += $" Server seconds  { DateTime.Now.Second.ToString() }";
        }
    }
}

<h2>Hello World</h2>
<p>
    @Model.Message
</p>

The sample page above is taken from the Microsoft Documents: Introduction to Razor Pages in ASP.NET Core 上面的示例页面摘自Microsoft文档:ASP.NET Core中的Razor页面简介

Has anyone figured this out, or know what is missing? 有没有人知道这个,或者知道缺少什么? I'm thinking there is an issue with the routing. 我在想路由问题。

It turns out there were two issues. 原来有两个问题。

(1) I needed to run the MVC middleware (duh!) (1)我需要运行MVC中间件(du!)

public void Configure(IApplicationBuilder app, ... )
{
    app.UseMvc();
}

(2) Then I got an exception thrown, which forced me to have to include .AddAuthorization() (2)然后引发异常,这迫使我必须包含.AddAuthorization()

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
        .AddAuthorization()
        .AddRazorPages();
}

Here it is super-simplified into a simple Console app: 在这里,它被超级简化为一个简单的控制台应用程序:

//using System.IO;
//using Microsoft.AspNetCore.Builder;
//using Microsoft.AspNetCore.Hosting;
//using Microsoft.Extensions.DependencyInjection;

public static void Main(string[] args)
{
    IWebHost host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureServices(services =>
        {
            services.AddMvcCore()
                .AddAuthorization()
                .AddRazorPages();
        })
        .Configure(app =>
        {
            //app.UseExceptionHandler("/error");
            app.UseStaticFiles();
            app.UseMvc();
        })
        .Build();

    host.Run();
}

Taking a look at the source code for AddMvc we can see that it calls AddMvcCore internally and then proceeds to add additional items. 看一下AddMvc源代码,我们可以看到它AddMvcCore内部调用AddMvcCore ,然后继续添加其他项。 So if I were you I would start adding these items in until you get Razor Pages to work, probably focusing on the Razor parts. 因此,如果我是您,我将开始添加这些项目,直到使Razor Pages起作用为止,可能主要关注Razor零件。 For example: 例如:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
        .AddViews()
        .AddRazorViewEngine()
        .AddRazorPages();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM