什么是发送从ASP.NET MVC操作的HTTP 404响应的正确方法? [英] What is the proper way to send an HTTP 404 response from an ASP.NET MVC action?

查看:137
本文介绍了什么是发送从ASP.NET MVC操作的HTTP 404响应的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给出的路线:

{FeedName} / {} ItemPermalink

例如:/博客/ HELLO-世界

ex: /Blog/Hello-World

如果该项目不存在,我想返回一个404什么是ASP.NET MVC这样做的正确方法?

If the item doesn't exist, I want to return a 404. What is the right way to do this in ASP.NET MVC?

推荐答案

从臀部拍摄(牛仔编码;-)),我建议是这样的:

Shooting from the hip (cowboy coding ;-)), I'd suggest something like this:

控制器:

public class HomeController : Controller
{
	public ActionResult Index()
	{
		return new HttpNotFoundResult("This doesn't exist");
	}
}



HttpNotFoundResult:

using System;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace YourNamespaceHere
{
    /// <summary>An implementation of <see cref="ActionResult" /> that throws an <see cref="HttpException" />.</summary>
    public class HttpNotFoundResult : ActionResult
    {
        /// <summary>Initializes a new instance of <see cref="HttpNotFoundResult" /> with the specified <paramref name="message"/>.</summary>
        /// <param name="message"></param>
        public HttpNotFoundResult(String message)
        {
            this.Message = message;
        }

        /// <summary>Initializes a new instance of <see cref="HttpNotFoundResult" /> with an empty message.</summary>
        public HttpNotFoundResult()
            : this(String.Empty) { }

        /// <summary>Gets or sets the message that will be passed to the thrown <see cref="HttpException" />.</summary>
        public String Message { get; set; }

        /// <summary>Overrides the base <see cref="ActionResult.ExecuteResult" /> functionality to throw an <see cref="HttpException" />.</summary>
        public override void ExecuteResult(ControllerContext context)
        {
            throw new HttpException((Int32)HttpStatusCode.NotFound, this.Message);
        }
    }
}
// By Erik van Brakel, with edits from Daniel Schaffer :)

使用这种方法您必须遵守的框架标准。目前市场上已经是有HttpUnauthorizedResult,所以这只会在其他开发人员眼中扩展框架以后维护你的code(你知道,谁知道你住在哪里的心理)。

Using this approach you comply to the framework standards. There already is a HttpUnauthorizedResult in there, so this would simply extend the framework in the eyes of another developer maintaining your code later on (you know, the psycho who knows where you live).

您可以使用反射,如果这种方式错过任何东西看看到装配看到HttpUnauthorizedResult是如何实现的,因为我不知道(这似乎太简单了差不多)。

You could use reflector to take a look into the assembly to see how the HttpUnauthorizedResult is achieved, because I don't know if this approach misses anything (it seems too simple almost).


我没有使用反射来看一看在HttpUnauthorizedResult刚才。似乎他们设置状态code对应对0x191(401)。虽然这适用于401,使用404作为新的价值,我似乎在Firefox中越来越只是一个空白页。 Internet Explorer中显示了一个默认的404,虽然(而不是ASP.NET版本)。使用工具栏webdeveloper视察我在FF的头,这确实显示404未找​​到响应。可以简单的东西我在FF配置错误。

I did use reflector to take a look at the HttpUnauthorizedResult just now. Seems they're setting the StatusCode on the response to 0x191 (401). Although this works for 401, using 404 as the new value I seem to be getting just a blank page in Firefox. Internet Explorer shows a default 404 though (not the ASP.NET version). Using the webdeveloper toolbar I inspected the headers in FF, which DO show a 404 Not Found response. Could be simply something I misconfigured in FF.


这是说,我认为杰夫的做法是KISS的一个很好的例子。如果你并不真的需要这个样本中的冗长,他的方法能正常工作为好。

This being said, I think Jeff's approach is a fine example of KISS. If you don't really need the verbosity in this sample, his method works fine as well.

这篇关于什么是发送从ASP.NET MVC操作的HTTP 404响应的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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