asp.net-mvcjsonfilterattribute

Need some help with a custom ASP.NET MVC IExceptionFilter


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.

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>

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;
    }
}

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?

Please help!


Solution

  • 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.