locked
How can I return a 404 status code by app.UseStatusCodePages? RRS feed

  • Question

  • User1052024640 posted

    These days I am handling 404 Not Found by Asp.Net Core .

    As we know, there are several ways to achieve this: app.UseStatusCodePagesWithRedirects/app.UseStatusCodePagesWithReExecute/app.UseStatusCodePages .

    I have to choose the app.UseStatusCodePages for I need to localizer the page by URL.

    For example:

    https://www.microsoft.com/en-us/microsoft-365/

    The 'en-us' in URL just for deciding the language of the page.

    Here is my code in startup.cs

    app.UseStatusCodePages(async context =>
                {
                    string currentCulture = "";
                    if (context.HttpContext.Request.Path.Value.Split('/').Length < 2)
                    {
                        currentCulture = "en";
                    }
                    else
                    {
                        string LanguageGet = context.HttpContext.Request.Path.Value.Split('/')[1];
                        currentCulture = SupportedCultures.Find(X => X.Name == LanguageGet) == null ? "en" : SupportedCultures.Find(X => X.Name == LanguageGet).Name;
                    }
                    var redirectPath = "/" + currentCulture + "/Error/" + context.HttpContext.Response.StatusCode;
                    context.HttpContext.Response.Redirect(redirectPath);
                });

    Now it works.

    However, after I input a URL which does not exist. The header shows a 302 status code in the Network of Chrome DevTools but not a 404 status code.

    Well, when I input the URL(https://www.microsoft.com/en-us/123.html), the header shows a right 404 status code in the Network of Chrome DevTools.

    I searched about this on Google. Someone said I should add a ProducesResponseType in the controller, just like this:

    public class OthersController : Controller
        {
            [Route("{culture=en}/Error/{code:int}")]
            [ProducesResponseType(StatusCodes.Status404NotFound)]
            public IActionResult Error(int code)
            {            
                return View(code);
            }
        }

    Well, it doesn't work any. I want to solve this because of SEO.

    How can I solve this? Thank you.

    Monday, May 4, 2020 9:04 AM

All replies

  • User-854763662 posted

    Hi mywatermelon,

    However, after I input a URL which does not exist. The header shows a 302 status code in the Network of Chrome DevTools but not a 404 status code.

    Well, when I input the URL(https://www.microsoft.com/en-us/123.html), the header shows a right 404 status code in the Network of Chrome DevTools.

    I fail to reproduce your issue , could you share the input value ,  the corresponding screenshot of result  and the expected result?

    Best Regards,

    Sherry

    Tuesday, May 5, 2020 7:26 AM
  • User1052024640 posted

    I am afraid the ASP.NET FORUMS can not upload the screen capture. I can only upload the screen capture to my onedrive:

    Here is the screen capture which is 302 status code by my way(The URL is localhost:4387/en/123.html):

    https://1drv.ms/u/s!AtcqjirAlaGYiuUxItkwex6Y4HcNPQ?e=1fjfl0

    Here is the right one which is 404 status code in Microsoft official website(The URL is http://www.microsoft.com/en-us/123.html):

    https://1drv.ms/u/s!AtcqjirAlaGYiuUwp_eEMOXktL3skA?e=v1fzQy

    And here is my source code:

    https://1drv.ms/u/s!AtcqjirAlaGYiuUyWqy8gzpgPF9U1Q?e=8WTYln

    Tuesday, May 5, 2020 7:55 AM
  • User1052024640 posted

    Hi, Sherry. I have post all the source codes before. Would you please help me? Thank you.

    Saturday, May 9, 2020 12:32 AM
  • User-854763662 posted

    Hi mywatermelon ,

    Here is the right one which is 404 status code in Microsoft official website(The URL is http://www.microsoft.com/en-us/123.html):

    What is the Microsoft official website you referenced?

    context.HttpContext.Response.Redirect(redirectPath);
    

    From the Microsoft official doc URL Rewriting Middleware in ASP.NET Core and UseStatusCodePagesWithRedirects , if you don't specify the status code, the Redirect method sends the status code defaults to 302 - Found which indicates that the resource is temporarily moved or replaced to client, and redirects the client to the location provided in the URL template.

    Best Regards,

    Sherry

    Thursday, May 14, 2020 7:32 AM
  • User1052024640 posted

    Em...now the problem is the redirect.

    However, is there any way to render the 404 pages but not redirect?

    I can hardly found another way to achieve this.

    Thursday, May 14, 2020 8:35 AM