【问题标题】:Razor Pages/ ASP.NET CORE Routing Issue on Deployed Site (404 Error)已部署站点上的 Razor Pages/ASP.NET CORE 路由问题(404 错误)
【发布时间】:2019-10-16 17:22:56
【问题描述】:

当我部署我的网站时,它会更改为某些项目生成的锚链接。在我的本地服务器上,链接原来是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();

    }
}

【问题讨论】:

    标签: c# asp.net asp.net-core .net-core razor-pages


    【解决方案1】:

    它认为你正在使用主控制器和索引视图。

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

    【讨论】:

    • 我认为您可能是对的,但我对如何进行更改感到困惑。我用包含我的路由信息​​的 Starup.cs 文件更新了原始帖子。
    • 你是控制器RecipesService 并且在那个类中有一个recipe 方法吗?所以它会是 asp-controller= recipesservice asp-action= recipe
    • RecipeService 是继承了 IRecipesService 接口的模型。我将它更改为 asp-controller 和 asp-action,但它不起作用。
    • 另一个问题是为什么它在本地服务器上工作但在部署时不能在天蓝色上工作?
    【解决方案2】:

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

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

    【讨论】:

    • 我之前改成那个了,还是不行。我又试了一次,还是不行。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2020-10-27
      • 2018-02-17
      • 1970-01-01
      • 2021-05-04
      • 2019-07-05
      • 2020-11-21
      • 2018-12-25
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多