我正在尝试在Web API中缓存ApiController方法的输出.
这是控制器代码:
public class TestController : ApiController
{
[OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public string Get()
{
return System.DateTime.Now.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
NB我还尝试了控制器本身的OutputCache属性,以及它的几个参数组合.
该路线在Global.asax中注册:
namespace WebApiTest
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute("default", routeTemplate: "{controller}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个成功的回复,但它没有缓存在任何地方:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jul 2012 17:56:17 GMT
Content-Length: 96 …Run Code Online (Sandbox Code Playgroud) 我有一个用ASP.NET编写的Web API,我通过AngularJS消费$http.
我已经在我的AngularJS工厂中启用了缓存,如下所示,但每个请求仍然返回响应200,从不200 (from cache)或304(并且每个请求我的意思是在同一页面上多次发出相同的web api请求,重新访问我已经访问过的页面包含Web API请求,刷新所述页面等).
angular.module('mapModule')
.factory('GoogleMapService', ['$http', function ($http) {
var googleMapService = {
getTags: function () {
// $http returns a promise, which has a 'then' function, which also returns a promise
return $http({ cache: true, dataType: 'json', url: '/api/map/GetTags', method: 'GET', data: '' })
.then(function (response) {
return response.data;
});
};
return googleMapService;
}]);
Run Code Online (Sandbox Code Playgroud)
我是否遗漏了AngularJS方面的一些东西?或者这是一个Web API问题.或两者?