NotFound()似乎无法正常工作 [英] NotFound() doesn't seem to work as expected

查看:123
本文介绍了NotFound()似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET内核的新手,所以我希望您对此感到满意.这类似于这个未回答的问题.

I'm new to ASP.NET core, so I hope you bear with me on this one. This is similar to this unanswered question.

当我测试一个全新的C#MVC项目时,如果输入错误的URL,则没有任何信息.只是一个空白页.

When I'm testing a brand new C# MVC project, and I enter a wrong URL, there's no info. Just a blank page.

为解决此问题,我改编了startup.cs,添加了UseStatusCodePagesWithReExecute()以返回静态404.html页面.这行得通.

To solve this, I have adapted startup.cs adding a UseStatusCodePagesWithReExecute() to return a static 404.html page. This works.

到目前为止,很好.

现在,我正在编写基本的登录逻辑.出于测试目的,缺少post参数时,我将调用return NotFound();.这不会返回任何内容.我知道404响应不正确,但是我在生成的代码中看到NotFound();,这就是返回空白页的内容,因此我想在继续之前解决该问题.

Now, I am coding a basic login logic. For testing purposes, I'm calling return NotFound(); when the post parameters are missing. This doesn't return anything. I'm aware that 404 is not the correct response, but I have seen NotFound(); in the generated code and I think that's what's returning blank pages, so I want to solve that before moving on.

似乎未为此调用app.UseDeveloperException();.我不确定如何测试.

The app.UseDeveloperException(); doesn't seem to be called for this. I'm not sure how to test that.

是否有一种方法可以覆盖NotFound();行为以某种方式获取404.html?

Is there a way to override the NotFound(); behavior to get a 404.html somehow?

这是

This is the Tutorial I have used to set up my project.

根据Alexander Powolozki的评论,我已将NotFound();替换为Redirect("~/404.html");.这行得通.

Based on the comments of Alexander Powolozki, I have replaced NotFound(); with Redirect("~/404.html");. This works.

// Wherever you want to return your standard 404 page
return Redirect("Home/StatusCode?code=404");


public class HomeController : Controller
{
    // This method allows for other status codes as well
    public IActionResult StatusCode(int? code)
    {
        // This method is invoked by Startup.cs >>> app.UseStatusCodePagesWithReExecute("/Home/StatusCode", "?code={0}");
        if (code.HasValue)
        {
            // here is the trick
            this.HttpContext.Response.StatusCode = code.Value;
        }

        //return a static file.
        try
        {
            return File("~/" + code + ".html", "text/html");
        }
        catch (FileNotFoundException)
        {
            return Redirect("Home/StatusCode?code=404");
        }
    }
}

推荐答案

在对问题进行了更多研究之后,我发现了一种无需使用Alexander Powolozki建议的使用重定向的方法.

After working a little more on the question, I have found a way to do it without using Redirect as suggested by Alexander Powolozki.

我创建了一个Utilities类以返回404代码和404.html.

I have created a Utilities class to return the 404 code and 404.html.

在HomeController中,我具有StatusCode IActionResult.

In the HomeController, I have the StatusCode IActionResult.

// This method is invoked by Startup.cs >>> app.UseStatusCodePagesWithReExecute("/Home/StatusCode", "?code={0}");
public IActionResult StatusCode(int? code)
{
    // Call the static method
    return Utilities.StatusCode(code: code.Value, this.HttpContext);
}

在模型中,我添加了一个Utilities.cs.

In Models, I've added a Utilities.cs.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.IO;

namespace MediaMgt.Models
{
    public static class Utilities
    {
        public static IActionResult StatusCode(int code, HttpContext httpContext)
        {
            // here is the trick
            httpContext.Response.StatusCode = code;

            //return a static file.
            try
            {
                return new VirtualFileResult("~/" + code + ".html", "text/html");
            }
            catch (FileNotFoundException)
            {
                httpContext.Response.StatusCode = 404;
                return new VirtualFileResult("~/404.html", "text/html");
            }
        }
    }
}

这样做而不是使用Redirect方法的原因是为了避免使用302往返客户端.此方法同时返回404错误代码和标准404.html.

The reason to do it like this instead of using the Redirect method is to avoid a round-trip to the client with a 302. This method returns both the 404 error code and a standard 404.html.

您可以从此处轻松添加更多功能.

You can easily add more functionality from here on.

这篇关于NotFound()似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆