ASP.NET MVC中的HttpException有什么意义 [英] What is the point of HttpException in ASP.NET MVC

查看:264
本文介绍了ASP.NET MVC中的HttpException有什么意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ASP.NET MVC 4中的HttpException异常类有点困惑。我理解它允许你抛出自定义错误代码,但是还有另一个接受消息的构造函数:

I'm a little bit confused with the HttpException Exception class in ASP.NET MVC 4. I understand it lets you throw custom error codes, but there's also another constructor that accepts a message:

HttpException构造函数

据我所知,HTTP只发回状态代码和与状态代码相关的小消息,如内部服务器错误。如果是这种情况,甚至在ASP.NET MVC应用程序的上下文中使用此类的意义何在?现在我有控制器,它使用特定的状态代码抛出HttpExceptions,告诉客户端发生了什么。这是正确的方法吗?示例:

As I understand, HTTP only sends back status codes and a small message related to the status code like 'Internal Server Error'. If this is the case, what is even the point of using this class in the context of an ASP.NET MVC app? Right now I have controllers which throw HttpExceptions with specific status codes to tell the client what is going on. Is this the right approach? Example:

    /// <summary>
    /// GET: Returns the data required for the modal to edit the environments.
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public ActionResult EditEnvironments()
    {
        try
        {
            List<Setting> settings = _service.GetAllSettings().ToList();
            return PartialView("_EditEnvironments", settings);
        }
        catch (Exception ex)
        {
            Log.Error(ex.Message, ex);
            throw new HttpException(500, "Something bad happened.", ex);
        }
    }

这里有消息发生了一件坏事。绝不会向客户展示。谢谢你的澄清!

Here the message "Something bad happened." will never be shown to the client. Thanks for the clarification!

推荐答案

根据评论,结果证明它在ASP.NET MVC应用程序中毫无意义客户端和服务器。两个Web API相互通信很有用。但是,如果有人感兴趣你想在ASP.NET MVC的上下文中传递消息到客户端,你可以像这样使用HttpStatusCodeResult:

Ok based on the comments it turns out that it's pointless in an ASP.NET MVC app with just a client and server. It's useful for two web APIs to talk to each other. However, if anyone is interested you want to 'pass' a message to the client in the context of ASP.NET MVC, you can use the HttpStatusCodeResult like this:

return new HttpStatusCodeResult(500,"Something bad happened")

你可以使用像这样的jQuery $ .ajax()访问发生了不好的事情:

And you can access "Something bad happened" using, say, jQuery $.ajax() like this:

                    $.ajax: {
                        url: "@Url.Action("RequestsAdminAjax", "Admin")",
                        type: "POST",
                        data: function(data) { return JSON.stringify(data); },
                        contentType: "application/json; charset=utf-8",
                        error: function (xhr, textStatus,errorThrown) {
                            debugger;
                            toggleAlert('<strong>Error: </strong>Unable to load data.', 'alert alert-danger');
                        }
                    },

errorThrown 将包含发生了不好的事情。

and errorThrown will contain "Something bad happened".

希望这可以帮到某人。

这篇关于ASP.NET MVC中的HttpException有什么意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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