剃刀页面锚标记生成错误的URL

时间:2018-07-28 08:20:10

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

我正在尝试使用剃须刀页面创建一个简单的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>

structure

1 个答案:

答案 0 :(得分:1)

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

提供的回购示例包含以下 .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>

在我的研究中,我遇到了以下问题

  

<Folder Include="..." />仅代表项目内的一个空文件夹。它在项目文件夹下不包含任何文件。

在创建文件夹时,项目将节点添加到项目文件中,但在添加文件后未将其删除。因此,在编译时未包含该文件夹下添加的剃须刀页面,这在运行时导致了所描述的问题。

建议从 .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。

相关问题