【发布时间】:2014-08-25 13:09:43
【问题描述】:
提前致谢,
我需要一个返回 JsonResult 的 MVC 控制器 (=GetFreqSuggestions) 来提供 Typeahead javascript 函数的 prefetch。
$(function() {
var bestPictures = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch:{
url: '../Reports/GetFreqSuggestions',
ttl:0
},
remote: '../Reports/ToDo?q=%QUERY'
});
bestPictures.initialize();
$('#remote .typeahead').typeahead({
hint: true,
highlight: true,
},
{
name: 'best-pictures',
displayKey: 'value',
source: bestPictures.ttAdapter()
});
});
Json 文件是从 azure blob 存储 作为 memorystream 获得的:
public async Task<bool> DownloadStreamFromBlobAsync(MemoryStream memStr, string blobContainerName, string blobName, ILogger log)
{
Stopwatch timespan = Stopwatch.StartNew();
try
{
CloudBlobContainer container = GetCloudBlob(blobContainerName);
if (container != null & memStr!=null)
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
await blockBlob.DownloadToStreamAsync(memStr);
}
}
catch (Exception e)
{
log.Error(.....);
return false;
}
timespan.Stop();
log.TraceApi(.......);
return true;
}
在控制器中,我想将包含先前上传的 json 文件的内存流转换为 jsonresult。
public async Task<JsonResult> GetFreqSuggestions()
{
MemoryStream mem = await _suggestoinProvider.DownloadFrequentSuggestionsAsync();
if(mem != null)
{
return ????
}
return null;
}
欢迎所有建议。
(我已经用return Json(data, JsonRequestBehavior.AllowGet); 进行了测试,当数据不是流时它可以完美运行)
我不喜欢先在服务器上保存 Json 文件。
【问题讨论】:
标签: c# asp.net-mvc json azure-blob-storage memorystream