无法部署 Razor Pages 网站

Cannot deploy Razor Pages website

我构建了一个简单的 Razor Pages 网站,以显示关于几个数据库表的一些视图。

index.cshtml.cs 我只有:

public class IndexModel : PageModel
{
    public IActionResult OnGet()
    {
        return RedirectToPage("./Registrations/Index");
    }
}

Registrations/index.cshtml.cs 我有:

public class IndexModel : PageModel
{
    int pageSize = 30;

    private readonly RegistrationAdmin.PostgresDbContext _context;

    public IndexModel(RegistrationAdmin.PostgresDbContext context)
    {
        _context = context;
    }

    public PaginatedList<Registration> Registration { get; set; }

    public async Task OnGetAsync(int pageIndex = 1)
    {
        IQueryable<Registration> registrations = _context.Registrations
            .Include(r => r.ContactPerson)
            .Include(r => r.Badge)
            .OrderByDescending(r => r.RegistrationId);

        this.Registration = await PaginatedList<Registration>.
                            CreateAsync(registrations.AsNoTracking(), pageIndex, pageSize);
    }
}

从 Visual Studio 测试网站没问题,使用 dotnet run 从项目目录 运行ning 也很好。

所以我部署了网站:

dotnet publish -o ../Published/ -c release

然后 运行 它与来自发布目录的 dotnet MyWebsite.dll

当我连接到 http://localhost:5000 时,出现一般错误。

查看控制台这是抛出的异常:

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
  An unhandled exception has occurred while executing the request.
System.InvalidOperationException: No page named './Registrations/Index' matches the supplied values.
   at Microsoft.AspNetCore.Mvc.Infrastructure.RedirectToPageResultExecutor.ExecuteAsync(ActionContext context, RedirectToPageResult result)
   at Microsoft.AspNetCore.Mvc.RedirectToPageResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)

我不明白部署的版本有什么问题。

根据错误消息,应用无法看到引用的视图。

默认情况下,项目在发布时会预编译页面。

引用 Publishing and deploying a Razor Pages application to IIS on Windows

尽管您已表明您在 mac

It is possible to skip this step and to publish the raw .cshtml files, resulting in pages that are updateable in a similar way to PHP, classic ASP or the ASP.NET Web Pages frameworks. In other words, you can make changes to the .cshtml files and then copy them to the web server while the application is running, and the new content will take effect immediately.

If you would like to adopt this approach, add an MvcCompileOnPublish node to your .csproj file, with the value set to false:

<PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>

This will result in a Pages folder containing the content pages and a refs folder containing the libraries required for the application

使用上述方法,同时更多的变通方法将允许您将缺失的视图复制到已发布的应用程序,并在发布时使它们可供使用。

编辑:

所以最后看起来 csproj 文件有问题,发布时排除了页面。

我们能够通过禁用页面预编译来确认,并看到问题页面在发布时没有被复制(考虑)。

清理 csproj 文件并重新编译解决了预编译和未编译复制页面的问题。

它最初直接在项目中工作,因为项目 运行 时所有必需的文件都存在。