gpt4 book ai didi

c# - 无法部署 Razor Pages 网站

转载 作者:太空狗 更新时间:2023-10-29 23:28:34 35 4
gpt4 key购买 nike

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

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 从项目目录运行也没有问题。

所以我部署了网站:

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)

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

最佳答案

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

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

引用 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

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

编辑:

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

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

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

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

关于c# - 无法部署 Razor Pages 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51390978/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com