Tôi gặp phải một loạt vấn đề và tìm ra giải pháp cho vấn đề 2 (tắt bộ nhớ đệm bất kể cài đặt mặc định).
// This forces the server to not provide any caching by refreshing its cache table immediately (0 sec)
[HttpCacheRefreshPolicy(0)]
// This forces the client (browser) to not cache any data returned from the server (even if ETag is present) by setting the time-out to 0 and no-cache to true.
[HttpCacheControlPolicy(true, 0, true)]
public void MyController : ApiControler {... }
Các thuộc tính phải được áp dụng cùng nhau để hoạt động. Bạn cũng có thể kiểm soát bộ nhớ đệm ở cấp độ hành động bằng cách cung cấp các quy tắc tương tự cho mỗi hành động.
Tôi vẫn chưa tìm ra giải pháp cho vấn đề 1. nhưng hãy xem không gian này để biết các bản cập nhật.
Cập nhật Tôi đã tìm thấy một giải pháp cho vấn đề 1.
- Đăng ký CachingHandler với container IoC của bạn (trong trường hợp của tôi nó
IUnityContainer
)
- Chích ICachingHandler vào điều khiển Web API của bạn.
- Để vô hiệu hóa tài nguyên, sử dụng
ICachingHandler.InvalidateResource(HttpRequestMessage)
Hãy xem một ví dụ mã dưới đây. Dung dịch đã được thử nghiệm.
pubic class Bootstrapper
{
//...
// Create a new caching handler and register it with the container.
public void RegisterCache(HttpConfiguration config, IUnityContainer container)
{
var cachingHandler = new CachingHandler(config);
// ...
container.RegisterInstance<ICachingHandler>(cachingHandler);
}
}
public class ResourceContoller : ApiController
{
private ICachingHandler _cachingHandler;
public ResourceContoller(ICachingHandler cachingHandler)
{
_cachingHandler = cachingHandler;
}
[HttpPost]
public void DeleteResource(int resourceId)
{
// Do the delete
// ...
// Now invalidate the related resource cache entry
// Construct a http request message to the related resource
// HINT: The "DefaultApi" may not be your api route name, so change this to match your route.
// GOTCHA: The route matching mechanism is case sensitive, so be aware!
var relatedResource = new HttpRequestMessage(HttpMethod.Get, Url.Link("DefaultApi", new {controller = "linkedresource", action = "getlinkedresource", id: resourceId}));
// Invalidate the resource with the caching handler.
_cachingHandler.InvalidateResource(relatedResource);
}
}
Nguồn
2014-08-22 01:18:31
Hi, xin lỗi vì đã không nhận được lại cho bạn, bạn vẫn còn có vấn đề? – Aliostad