问题描述
我试图使自己的ExceptionFilter。开箱即用,ASP.NET MVC自带[的HandleError]属性。这是伟大的 - >但它返回一些HTML错误查看
i'm trying to make my own ExceptionFilter. Out of the box, ASP.NET MVC comes with the [HandleError] attribute. This is great -> but it returns some html error View.
因此,我想返回一些JSON的错误消息。所以我做我自己的。
As such, I'm wanting to return some json error message. So i'm making my own.
现在,直到我测试我的网址一切都很正常。我不断收到一个错误。这是消息....
Now, everything works great until i test my url. I keep getting an error. this is the message....
C:\Temp\curl-7.19.5>curl -i http://localhost:6969/search/foo?name=1234&key=test1xxx
HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 14 Sep 2009 01:54:52 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 6
Connection: Close
"Hi StackOverflow"'key' is not recognized as an internal or external command,
operable program or batch file.
C:\Temp\curl-7.19.5>
确定 - 这是没有意义的。让我们让一些code解释什么,我试图做的,然后...
Ok - that makes no sense. Lets let at some code to explain what i'm trying to do, then...
public class HandleErrorAsJson : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// Snip normal checks and stuff...
// Assume we've figured out the type of error this is.
// I'm going to hardcode it here, right now.
int statusCode = 401;
string message = "Hi StackOverflow";
// Now prepare our json output.
filterContext.Result = new JsonResult
{
Data = message
};
// Prepare the response code.
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = statusCode;
}
}
这就是我的code ....和它的工作八九不离十,但事实并非如此。
So that's my code .... and it's sorta working but it's not.
这是什么'关键'的事情是什么意思?我有什么遗漏,意欲何为?
What does this 'key' thing mean? what have i missed, trying to do?
请帮帮忙!
推荐答案
找到我的答案 - >我只好把网址中的..(引号),否则它试图运行什么是符号符号之后,作为命令或东西。
Found my answer -> i had to put the url in ".." (quotes) otherwise it tries to run whatever is after the ampersand symbol, as a command or something.
不知道为什么,但修复它。
not sure why, but that fixes it.
这篇关于需要一些帮助与自定义ASP.NET MVC个IExceptionFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!