【问题标题】:Blazor return 404 as headerBlazor 返回 404 作为标题
【发布时间】:2021-01-12 08:42:11
【问题描述】:

所以我尝试使用 Blazor(不是 WebAssembly)一段时间并坚持使用重定向和标题响应。

Post.cs:

    public class PostService
    {
        public List<Post> posts = new List<Post>()
        {
            new Post { ID = 1, Title = "First title", Text = "first text", Date = DateTime.Now, Author = "JohnDoe", Category = "Good" },
            new Post { ID = 2, Title = "Second title", Text = "second text", Date = DateTime.Now, Author = "JohnDoe", Category = "Nice" }
        };
        public Task<List<Post>> GetPosts()
        {
            return Task.FromResult(posts);
        }
        public Task<Post> GetPost(int id)
        {
            Post post = posts.Where(x => x.ID == id).FirstOrDefault();
            return Task.FromResult(post);
        }
    }

BlogPost.razor:

@page "/post/{ID:int}"
@using ExampleBlog.Data;
@inject PostService PostClassService
@inject NavigationManager _navigationManager

@if (post == null)
{
    <p>Loading</p>
}
else
{
    @post.Text
}
@code {
    [Parameter]
    public int ID { get; set; }
    public Post post { get; set; }
    public bool found { get; set; } = true;

    protected override async Task OnInitializedAsync()
    {
        post = await PostClassService.GetPost(ID);
        if (post == null)
        {
            found = false;
        }
    }
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            if (!found)
            {
                _navigationManager.NavigateTo("404");
            }
        }
    }
}

一切都很好,如果 ID 正确,它会显示 post.Text 并正确重定向到 404 在浏览器中

问题是服务器头返回:

curl -i --insecure https://localhost:44332/post/1
HTTP/2 200

没关系,工作正常,但如果我尝试访问不存在的实体:

curl -i --insecure https://localhost:44332/post/1234
HTTP/2 200

我需要返回 404 服务器端,以便搜索引擎等能够正确识别所有内容。

【问题讨论】:

    标签: c# http iis blazor


    【解决方案1】:

    它没有返回 not found 因为它实际上是在寻找页面路由签名Post(int)。 尝试在其中设置一个断点。

    你需要类似的东西

    public Task<Post> GetPost(int id)
     {
         Post post = posts.Where(x => x.ID == id).FirstOrDefault();
         if (post == null)
         { 
             return NotFound();
         }
         else
         {
             return Task.FromResult(post);
         }
     }
    

    【讨论】:

      【解决方案2】:

      所以我发现 bad 解决方法可以正常工作。

      protected override async Task OnInitializedAsync()
      {
          post = await PostClassService.GetPost(ID);
          if (post == null)
          {
              found = false;
      
              #if !DEBUG
                  _navigationManager.NavigateTo("404");
              #endif
      }
      

      #debug 是必需的,因为 blazor 错误会在您处于调试状态时向 NavigationManager 抛出异常。

      并在 Startup.cs 中更改了 EndPoints

              app.UseEndpoints(endpoints =>
              {
                  endpoints.MapBlazorHub();
                  endpoints.MapFallbackToPage("/_Host");
                  endpoints.MapGet("/404", async context =>
                  {
                      context.Response.StatusCode = 404;
                      await context.Response.WriteAsync("404 not found");
                  });
              });
      

      如果我在发行版中运行它,一切正常(除了它返回未找到 404 的空白页)

      curl -i --insecure https://localhost:44332/post/1 
      HTTP/2 200
      
      curl -i --insecure https://localhost:44332/post/1234
      HTTP/2 302
      content-type: text/html; charset=utf-8
      location: https://localhost:44332/404
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 2020-07-10
        • 2016-12-10
        • 2014-07-02
        • 2019-12-16
        相关资源
        最近更新 更多