Asp.net-Core

相對頁面路徑“關於”只能在執行 Razor 頁面時使用。指定一個

  • March 28, 2019

首先,我搜尋了此錯誤,但一無所獲。完整的例外:

   System.InvalidOperationException: The relative page path 'About' can only be used while executing a Razor Page. Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page.
  at Microsoft.AspNetCore.Mvc.UrlHelperExtensions.CalculatePageName(ActionContext actionContext, String pageName)
  at Microsoft.AspNetCore.Mvc.UrlHelperExtensions.Page(IUrlHelper urlHelper, String pageName, String pageHandler, Object values, String protocol, String host, String fragment)
  at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GeneratePageLink(ViewContext viewContext, String linkText, String pageName, String pageHandler, String protocol, String hostname, String fragment, Object routeValues, Object htmlAttributes)
  at Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Process(TagHelperContext context, TagHelperOutput output)
  at Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
  at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
  at AspNetCore.Views_Shared__FooterPartial.ExecuteAsync()
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
  at Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper.RenderPartialViewAsync(TextWriter writer, Object model)
  at Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
  at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
  at AspNetCore.Views_Shared__Layout.<ExecuteAsync>b__40_1() in D:\Documents\Obaju\Obaju.App\Views\Shared\_Layout.cshtml:line 40
  at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
  at AspNetCore.Views_Shared__Layout.ExecuteAsync()
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext context, ViewBufferTextWriter bodyWriter)
  at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
  at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
  at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
  at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
  at Microsoft.AspNetCore.Mvc.ViewResult.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.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
  at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
  at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
  at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
  at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
  at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
  at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

About 是通過 /about url 路徑訪問的 Razor 頁面,它執行良好。僅當我嘗試繼續 /home/index 或另一個 mvc 視圖時才會發生此錯誤。

問題

在您的 Razor 視圖中,有一個看起來像這樣的錨標記:

<a asp-page="About">About</a>

該語法(沒有正斜杠/)僅適用於 Razor 頁面。

解決方案

在 Razor 視圖中,在頁面名稱前加上正斜杠/,如下所示:

<a asp-page="/About">About</a>

從 Razor 頁面中區分 Razor 視圖

  • Razor 視圖通常位於Views目錄中。
  • Razor 頁面通常位於Pages目錄中,並且@page在文件頂部有一個指令。

<a class="nav-link" asp-area="Identity" asp-page="/Account/Manage/ChangePassword">

要創建Razor 頁面連結,請添加asp-area,然後添加 asp -page。

引用自:https://stackoverflow.com/questions/52020774