Razor Pages/ ASP.NET 部署站点上的核心路由问题(404 错误)

Razor Pages/ ASP.NET CORE Routing Issue on Deployed Site (404 Error)

当我部署我的网站时,它会更改为某些项目生成的锚 link。在我的本地服务器上,link 结果是 http://localhost:49377/Recipe/1,当我单击它时它起作用了。在我部署的服务器上,它显示 thedeployedsserver.net/Home/Index/1?page=%2FRecipe 。

Menu.cshtml中的这个link发生了变化:

<h3><a asp-page="/Recipe" asp-route-id="@recipe.Id">@recipe.Name</a></h3>

我认为这可能是一些路由问题,但我不知道它是什么。我仍在浏览文档,但希望有人能帮助我或为我指明正确的方向。

我的 Menu.cshtml

中有以下代码
@page
@{
    var recipes = await RecipesService.GetAllAsync();
}
@section Title {
    <h2 class="title">My Favorite Recipes</h2>
}

<div class="row recipes">

    @foreach (var recipe in recipes)
    {
        <div class="recipe col-md-4 burgerBlackBoard">
            <img class="img burgerImage" src="@recipe.GetInlineImageSrc()" />
            <h3><a asp-page="/Recipe" asp-route-id="@recipe.Id">@recipe.Name</a></h3>
        </div>

    }
</div>

这是我的 Recipe.cshtml 页面:

@page "{id}"
@{
    var id = long.Parse((string)RouteData.Values["id"]);
    var recipe = await RecipesService.FindAsync(id);

    ViewData["Title"] = recipe.Name;
}

    <div class="row recipe burgerBlackBoard">
        <div class="col-6 ">
            <div class="col-8 text-center">
                <span class="description">
                    @recipe.Description
                </span>
            </div>
            <div class="col-2 text-center">
                <img class="img burgerImage" src="@recipe.GetInlineImageSrc()" />
            </div>
            <hr />
        </div>
        <div class="ingredients col-6">
            <h3 class="text-center">Ingredients</h3>
            <ul>
                @foreach (var ingredient in recipe.IngredientsList)
                {
                    <li>@ingredient</li>
                }
            </ul>
        </div>
    </div>

我的 startup.cs 保存我的路由:

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
        services.AddMvc().AddRazorPagesOptions(options =>
        {
            options.Conventions.AuthorizeFolder("/Admin");
            options.Conventions.AuthorizeFolder("/Account");
            options.Conventions.AllowAnonymousToPage("/Account/Login");
        }); //dependency injection
        services.AddTransient<IRecipesService, RecipesService>();


    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseAuthentication();
        app.UseStaticFiles();
        app.UseMvcWithDefaultRoute();

    }
}

它认为您正在使用家庭控制器和索引视图。

而不是 asp-page 您可以使用 asp-controller 和 asp-action。如果您分享您的路线设置,我可以提供更好的答案。

在你的 link 中将 /Recipe 更改为 ./Recipe

<h3><a asp-page="./Recipe" asp-route-id="@recipe.Id">@recipe.Name</a></h3>

已解决问题。似乎我有一个名为 Recipe 的单独模型和一个名为 Recipe 的页面,它们的名称相互冲突。我刚刚创建了另一个名为 Burgers 的页面,它解决了与路由问题的冲突。