如何在不浏览器提示来保存文件返回JSONResult? [英] How to return JSONResult without the browser prompting to save the file?
问题描述
编辑:我早已超越VS2008,我还没有与返回使用MVC 3+ JSON结果的任何问题。我想,以纪念这个问题已经过时,或者诸如此类的话。也许有人仍然会在这个发现的价值和给出答案,但我不能将它们标记为正确的,因为我没有测试它们的方法。
我新的MVC和我试图做一个简单的现场工作。我开始怀疑它是否真的值得吗?我能有这个网站建立和运行与老派ASP.Net两个或已经三次......但那是题外话;-)
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 ;-)
我怎样才能得到我的控制器无需浏览器提示我保存响应为文件返回JSONResult?这里是调用操作的JavaScript:
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
提琴手显示我的JSON被返回到浏览器:
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.
我从Visual Studio 2008 IDE内部运行这一点。我有什么做的,使这项工作,无论是在IDE和从IIS?
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?
在此先感谢!
推荐答案
只需设置内容类型为text / plain的
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
如果它不工作,你也可以继承的 JsonResult 的并重写的的ExecuteReuslt 的方法:
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";
}
}
这篇关于如何在不浏览器提示来保存文件返回JSONResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!