【问题标题】:Razor Pages anchor tag generating incorrect URLRazor Pages 锚标记生成不正确的 URL
【发布时间】:2019-01-05 06:12:21
【问题描述】:

我正在尝试使用剃须刀页面创建一个简单的 Web 应用程序。 Create 的渲染锚标记未正确生成。

<a href>Create</a>

我的应用程序似乎没有 /Facility/Create 作为有效 URL,即使我手动转到那里也是如此。我想还有什么其他事情可以让锚标记呈现为

<a href="/Facility/Create">Create</a>

并让应用程序了解/Facility/Create。

@page
@model IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<h1>Facilities</h1>
<form method="post">
    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var facility in Model.Facilities)
            {
            <tr>
                <td>@facility.Name</td>
                <td>
                    <a asp-page="Edit" asp-route-id="@facility.Id">edit</a>
                    <button type="submit" asp-page-handler="delete"
                            asp-route-id="@facility.Id">
                        delete
                    </button>
                </td>
            </tr>
            }
        </tbody>
    </table>
    <a asp-page="/Facility/Create">Create</a>
</form>

【问题讨论】:

  • 上面显示的标记属于哪个索引页面? Pages/Index.cshtml 或 Pages/Facility/Index.cshtml?
  • @Nkosi - 这是/Pages/Facility/Index.cshtml
  • 在这种情况下&lt;a asp-page="Create"&gt;Create&lt;/a&gt; 应该可以工作。如果没有,请检查实际的 .cshtml 文件是否被项目识别并且在构建时未被排除。在许多情况下,清除和重建项目可以解决问题。
  • @Nkosi - 我已经尝试过&lt;a asp-page="Create"&gt;、&lt;a asp-page="./Create"&gt; 和&lt;a asp-page="/Facility/Create"&gt; --- 我还进行了清理和重建并检查以确保它包含在构建中。我在/obj 中有一个.g.cs 文件
  • @Nkosi - 这是一个为我重现问题的仓库 - github.com/bdparrish/razor-pages-bug

标签: asp.net-core asp.net-core-2.0 razor-pages


【解决方案1】:

存在基于正在使用的 IDE 版本的工具问题,可能会损坏 .csproj 文件。

提供的 repo 示例具有以下 .csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Pages\Facility\" />
  </ItemGroup>
  <ItemGroup>
    <Content Update="appsettings.Development.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

在我的研究中,我遇到了以下情况

&lt;Folder Include="..." /&gt; 只代表项目中的一个空文件夹。它不包含项目文件夹下的任何文件。

在创建文件夹时,项目将节点添加到项目文件中,但在添加文件后并未将其删除。因此,在该文件夹下添加的 razor 页面在编译时未包含在内,这导致在运行时出现上述问题。

建议从 .csproj 中删除节点,因为项目文件夹中的所有文件都默认包含/嵌入/编译,除非在项目文件中特别选择退出。

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

  <ItemGroup>
    <Content Update="appsettings.Development.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

重新编译项目后,缺失的页面将可用并提供预期的行为。

最后检查以确保可用的补丁和更新应用到正在使用的 IDE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 2021-11-21
    • 2020-09-18
    • 2015-05-14
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多