繁体   English   中英

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

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

当我部署我的网站时,它会更改某些项目的生成锚链接。 在我的本地服务器上,链接原来是http://localhost:49377/Recipe/1 ,当我单击它时它就可以工作。 在我部署的服务器上,它显示了deployedsserver.net/Home/Index/1?page=%2FRecipe。

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

<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();

    }
}

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

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

在您的链接中将 /Recipe 更改为./Recipe

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM