我有一个page1.aspx:
<%@ Register src="uc1.ascx" tagname="UcHead" tagprefix="uc1" %>
Run Code Online (Sandbox Code Playgroud)
和uc1.ascx使用OutputCache:
<%@ OutputCache Duration="18000" VaryByParam="*" %>
Run Code Online (Sandbox Code Playgroud)
如何单击另一个page2.aspx中的按钮以删除OutputCacheuc1.ascx或page1.aspx?
当OutputCache在page1.aspx中时,我可以使用以下代码删除OutputCache:
string url = "/page1.aspx";
HttpResponse.RemoveOutputCacheItem(url);
Run Code Online (Sandbox Code Playgroud)
但是当OutputCache在uc1.ascx中时它不起作用.
在ASP.NET MVC中实现异步控制器操作时,如果我想输出缓存ActionResult,我将该OutputCache属性放在哪个方法上?
public class PortalController : AsyncController {
/// HERE...?
[OutputCache(Duration = 60 * 30 /* 30min */, VaryByParam = "city")]
public void NewsAsync(string city) {
AsyncManager.OutstandingOperations.Increment();
NewsService newsService = new NewsService();
newsService.GetHeadlinesCompleted += (sender, e) =>
{
AsyncManager.Parameters["headlines"] = e.Value;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetHeadlinesAsync(city);
}
/// ...OR HERE?
[OutputCache(Duration = 60 * 30 /* 30min */, VaryByParam = "city")]
public ActionResult NewsCompleted(string[] headlines) {
return View("News", new ViewStringModel
{
NewsHeadlines = headlines
});
} …Run Code Online (Sandbox Code Playgroud) 我想使用输出缓存来避免使用相同的静态查询一遍又一遍地访问我的数据库,但我的控制器具有唯一定义帖子的参数.
我如何考虑我的参数并仍然支持asp.net-mvc中的输出缓存?
使用下面给出的任何OutputCacheProfiles
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear/>
<add
name="CachingProfileParamEmpty"
duration="87"
varyByParam=""
location="Any"
/>
<add
name="CachingProfileParamNone"
duration="87"
varyByParam="None"
location="Any"
/>
<add
name="CachingProfileParamStar"
duration="87"
varyByParam="*"
location="Any"
/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Run Code Online (Sandbox Code Playgroud)
标题变化:*始终发送
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 05 Mar 2012 20:11:52 GMT
X-AspNetMvc-Version: 3.0
Cache-Control: public, max-age=87
Expires: Mon, 05 Mar 2012 20:13:13 GMT
Last-Modified: Mon, 05 Mar 2012 20:11:46 GMT
Vary: *
Content-Type: text/html; charset=utf-8
Content-Length: 5368
Connection: Close
Run Code Online (Sandbox Code Playgroud)
这反过来导致浏览器将请求发送到服务器而不是本地缓存.即使使用
this.Response.Cache.SetOmitVaryStar(false);
Run Code Online (Sandbox Code Playgroud)
没有帮助.我可以强制不发送标头的唯一方法是使用直接属性
[OutputCache(Duration = 60, VaryByParam = "", Location = …Run Code Online (Sandbox Code Playgroud) 如何在MVC中为下拉列表缓存我的项目和值?
有办法吗?
我在控制器中这样做.
示例代码是.......
public ActionResult Index()
{
RegionTasks regionTasks = new RegionTasks();
ViewBag.Region = GetRegions();}
Run Code Online (Sandbox Code Playgroud)
我的控制器具有如下功能.
[OutputCache(Duration = 10, Location = System.Web.UI.OutputCacheLocation.Server)]
private IEnumerable<SelectListItem> GetRegions()
{
RegionTasks regionTasks = new RegionTasks();
return regionTasks.GetRegions();
}
Run Code Online (Sandbox Code Playgroud)
我已经测试过它并没有缓存该区域的项目.
我怎样才能做到这一点?
这是我的控制器方法.任何人都可以解释我如何在服务器上为以下方法编写outputcache.
public JsonResult GetCenterByStateCityName(string name, string state, string city, bool sportOnly, bool rvpOnly)
{
var result = GetCenterServiceClient().GetCentersByLocation(name, city, state, sportOnly, rvpOnly).OrderBy(c => c.Name).ToList();
return Json(result);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我有Controller PartialViewResult或JsonResult返回类型.
我想缓存它[OutputCache],但它根本不工作,总是以下Index控制器Thread.Sleep(5000);运行!
[HttpPost]
[ValidateAntiForgeryToken]
[OutputCache(Duration = 120, Location = OutputCacheLocation.Server)]
public ActionResult Index(DevicesAjaxViewModel viewModel)
{
try
{
//Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
//Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.AddValidationCallback(IsCacheValid, Request.UserAgent);
#if DEBUG
Thread.Sleep(5000);
#endif
if (!ModelState.IsValid) return Json(new ModelError("Error in Model"));
var allObjects = _objectService.GetAllObjects();
string objectName = allObjects.First(q => q.Id == viewModel.ObjectId).Name;
KeyValuePair<int, List<DeviceModel>> keyValuePair = ApplyFiltering(objectName, viewModel.PageNumber, false, viewModel.Filtering);
FilteringDevicesResultModel filteringDevicesResultModel = new FilteringDevicesResultModel
{
Devices = keyValuePair.Value,
FoundDevicesCount = keyValuePair.Key.ToMoneyFormat(),
RequestId = viewModel.RequestId
};
return PartialView("~/Views/Partials/DevicesPagePartial.cshtml", …Run Code Online (Sandbox Code Playgroud) 我很清楚如何自己设置它,但我担心在两个不同的地方配置它。要获得赏金,请告诉我应该在哪里寻找现有设置。
我们已经寻找现有设置的地方,但没有成功:
<StaticContent>部分SetMaxAge)我们最近注意到我们的 CSS 和 JS 文件没有得到刷新。当我检查网络流量时,它们从服务器返回,并带有一个标头 ( Cache-Control: max-age=604800),该标头为它们提供了 7 天的生命周期。
我们需要减少缓存寿命。但对于我的生活,我找不到它的位置。
它没有在 web.config<StaticContent>部分中设置。
它没有在 IIS 输出缓存部分设置(我查看了机器、站点和应用程序——它们都是空白的)。
它没有在代码中设置——我进行了全局代码搜索SetMaxAge并得到了 nuthin'。我还能在哪里看?
它可能是由我们数据中心的网关或负载均衡器设置的吗?
图像我有一个使用OutputCache属性缓存的视图,但我仍然需要增加一个记录页面已被查看的计数器,我该怎么办呢?
我考虑创建自己的Custom ActionFilterAttribute,并使用Action Filter Order of Execution来记录这个..但我不确定它是否会起作用.
例如.
[IncrementViewCountFilter(Order=1)]
[OutputCache(Duration=60,Order=2)]
public ActionResult Index(int questionId)
{ ... }
Run Code Online (Sandbox Code Playgroud)
首先,我的假设是,如果调用OutputCache,并且页面被缓存,则不会运行控制器代码.
我猜测的下一个问题是IncrementViewCountFilter不会知道questionId,所以它如何知道要增加什么(因为它是在执行主索引代码之前执行的).
其次,如果IncrementViewCountFilter确实知道了问题..并且它获得了大量的点击,你不会希望它一直写到数据库......但只有当它达到某个数字时...然后你'冲洗' 输出.
有人有什么想法?
我正在尝试将一个OutputCache添加到具有WebImage.Write()响应的MVC Action,但是一旦我添加它(即使持续时间为0),内容类型也会从image/jpeg更改为text/html和I获取在浏览器中以文本形式呈现的图像.
示例代码 - 如果删除了OutputCache属性,这可以正常工作:
[OutputCache(Duration = 3000)]
public void GetImage(Guid id)
{
//Create WebImage from byte[] stored in DB
DbImage image = DbImageDAL.SelectSingle(e => e.DbImageId == id);
WebImage webimage = new WebImage(image.Data);
webImage.Write();
//Tried webImage.Write("JPEG"); but it makes not difference
}
Run Code Online (Sandbox Code Playgroud)