camainc
camainc

Reputation: 3810

How to return JSONResult without the browser prompting to save the file?

EDIT: I've long since moved beyond VS2008, and I haven't had any issues with returning JSON results using MVC 3+. I'd like to mark this question as obsolete, or something to that effect. Perhaps someone will still find value in this and the answers given, but I can't mark them as "correct" since I have no way of testing them.

I am new to MVC, and am trying to make a simple site work. I'm starting to wonder if it is really worth it...I could have had this site up and running with "old-school" ASP.Net two or three times already...but that is beside the point ;-)

How can I get my controller to return a JSONResult without the browser prompting me to save the response as a file? Here is the JavaScript that calls the action:

$("select#PageId").change(function() {
    var id = $("#PageId > option:selected").attr("value");
    $.getJSON('FindCategories/', { PageId: id }, 
        function(data) {
            if (data.length > 0) {
                var options = '';
                for (c in data) {
                    var cat = data[c];
                    options += "<option value='" + cat.CategoryId + "'>" + cat.CategoryName + "</option>";
                }
                $("#CategoryId").removeAttr('disabled').html(options);
            } else {
                $("#CategoryId").attr('disabled', true).html('');
            }
    });
});

Here is my controller action:

Function GetCategoriesByPage(ByVal PageId As Integer) As JsonResult

    Dim categories As List(Of Models.WebCategoryLite) = _service.ListCategoriesByPageId(PageId)

    Dim res As New JsonResult
    res.Data = categories
    Return res

End Function

Fiddler shows me that the JSON is being returned to the browser:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 24 Aug 2009 19:43:53 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 246
Connection: Close

[{"CategoryID":1,"CategoryName":"Sample Category"},{"CategoryID":2,"CategoryName":"Another Sample"},{"CategoryID":3,"CategoryName":"Yet Another Sample"}]

No matter what browser I try this with, I get a "save file as" prompt.

I am running this from inside the Visual Studio 2008 IDE. What do I have to do to make this work, both in the IDE and from IIS?

Thanks in advance!

Upvotes: 0

Views: 4186

Answers (2)

King
King

Reputation: 11

Good job.

var s = new JsonResult();
s.ContentType = "text/plain";
s.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
s.Data = AreaServiceClient.GetCityList(id);            
return s;

Upvotes: 1

Cleiton
Cleiton

Reputation: 18113

Just set Content-type to "text/plain":

Function GetCategoriesByPage(ByVal PageId As Integer) As JsonResult

    Dim categories As List(Of Models.WebCategoryLite) = _service.ListCategoriesByPageId(PageId)

    Dim res As New JsonResult
    res.Data = categories
    res.ContentType = "text/plain"
    Return res

End Function

If it doesn't work, you can subclass JsonResult and override ExecuteResult method:

public class myOwnJsonResul: JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        base.ExecuteResult(context);
        context.HttpContext.Response.ContentType = "text/plain";
    }
}

Upvotes: 2

Related Questions