我试图这样做,它导致contentlen 0的结果
看起来像:
[OutputCache(Duration = 36000)]
public JsonResult GetFileClasses()
{
return this.Json(TopicConfig.FileExtensionsSettings.List)
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已将Output Caching添加到我的应用程序中的几个操作中,以便轻松提升性能.但是,这些操作还需要通过命中Redis数据库在每个请求(它是一个视图计数器)后递增一个计数器.
首先,我想我可以调整动作过滤器执行的顺序,以确保计算视图:
public class CountersAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//increment my counter all clever like
base.OnResultExecuted(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
但那没用; 显然,OutputCacheAttribute的行为与普通的动作过滤器不同.然后我尝试实现自定义输出缓存:
public class OutputCacheWithCountersAttribute : OutputCacheAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//straight to the source to get my headcount!
base.OnResultExecuted(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
不,也没用; 缓存操作后,操作过滤器似乎完全被忽略.游民.
所以,呃,有没有办法(没有实现自定义输出缓存提供程序),以确保我的观点被正确计算,这是干净和明智的?
我在用户控件的基类上使用PartialCaching属性.
我希望缓存的控件根据控件实例上设置的属性而变化.
例如:
<mycontrols:control1 runat="server" param1="10" param2="20" />
Run Code Online (Sandbox Code Playgroud)
...输出将与具有不同属性的控件实例分开缓存:
<mycontrols:control1 runat="server" param1="15" param2="20" />
Run Code Online (Sandbox Code Playgroud)
...此控件也将单独缓存:
<mycontrols:control1 runat="server" param1="10" param2="25" />
Run Code Online (Sandbox Code Playgroud)
但是,如果两个单独页面上的两个控件实例具有相同的 param1和param2属性,我希望它们作为一个对象进行缓存(以便共享缓存控件).
可以使用PartialCaching属性实现上述用例吗?我会使用什么设置?varyByControl?
此外,是否可以在运行时使缓存持续时间变量?
谢谢.
我有一个Asp .Net Web应用程序,其中一些页面继承自基类"BasePage".这个BasePage类继承自System.Web.ui.Page并且我覆盖了Render方法,这样我就可以在将HTML流发送到客户端浏览器之前拦截它.
这是我的Render方法的代码:
protected override void Render(HtmlTextWriter writer){
MemoryStream memoryStream = new MemoryStream();
try
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream))
{
var textWriter = new HtmlTextWriter(streamWriter);
base.Render(textWriter);
textWriter.Flush();
memoryStream.Position = 0;
using (StreamReader reader = new StreamReader(memoryStream))
{
string finalHtml = reader.ReadToEnd();
// Processing filters
finalHtml = FilterManager.ProcessFilters(finalHtml);
// Writing output to client
writer.Write(finalHtml);
reader.Close();
}
}
}
catch(ObjectDisposedException)
{
}
finally
{
memoryStream.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.有用.我没有使用响应过滤功能的原因是我知道它与Post-cache替换不兼容
是.我也想使用outputcache替换.出于测试目的,我在aspx页面中添加了一个小的asp .net控件:
<asp:Substitution runat="server" id="UnCachedArea" methodname="GetFreshDateTime" /> …Run Code Online (Sandbox Code Playgroud) 我正在学习ASP.NET MVC并且被一个问题所困扰.
在HomeController中,Index操作具有OutputCache属性,但似乎不起作用.
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration=5, VaryByParam="none")]
public ActionResult Index()
{
ViewData["Title"] = "Home Page" + DateTime.Now;
ViewData["Message"] = "Welcome to ASP.NET MVC! " + DateTime.Now;
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
经过几分钟的尝试,我发现这是由于我访问动作的方式.如果我通过" http:// localhost:3573 / " 访问它,则outputcache不起作用.但是,如果我使用" http:// localhost:3575/Home/Index " 访问它,则outputcache可以正常工作.
任何人都知道任何解决方法,使默认的控制器 - 动作outputcacheable?
谢谢.
我正在尝试使用VaryByParam和VaryByHeader进行一些缓存.当一个AJAX请求进来时,我返回一个部分XHTML.当一个常规请求进来时,我发送带有页眉/页脚的部分XHTML页面.
我尝试通过执行以下操作来缓存页面:
[OutputCache( Duration = 5, VaryByParam = "nickname,page", VaryByHeader = "X-Requested-With" )]
Run Code Online (Sandbox Code Playgroud)
但是这不起作用...如果我首先执行常规请求然后运行AJAX调用我得到完整的缓存页面而不是部分,反之亦然.似乎VaryByHeader被忽略了.是因为正常请求中省略了X-Requested-With?或者它正在做VaryByParam或VaryByHeader?
我明显的方法是让AJAX请求调用一个只返回部分页面的不同方法,但是如果可能的话我想避免这种情况.
我正在使用带有OutputCacheAttribute的 ASP.NET MVC 1.0 .
一个非常基本的http请求:
GET http://stackoverflow.com/feeds/tag?tagnames=c%23&sort=newest HTTP/1.1
Host: stackoverflow.com
Accept-Encoding: gzip,deflate
Run Code Online (Sandbox Code Playgroud)
击中路线装饰:
[OutputCache(Duration = 300, VaryByParam = "tagnames;sort",
VaryByContentEncoding = "gzip;deflate", VaryByCustom = "site")]
Run Code Online (Sandbox Code Playgroud)
如果你包括if-modified-since,或者200 的旧数据,即重复和错误地服务304(无变化),即
HTTP/1.1 200 OK
Cache-Control: public, max-age=0
Content-Type: application/atom+xml; charset=utf-8
Content-Encoding: gzip
Expires: Fri, 01 Jul 2011 09:17:08 GMT
Last-Modified: Fri, 01 Jul 2011 09:12:08 GMT
Vary: *
Date: Fri, 01 Jul 2011 09:42:46 GMT
Content-Length: 14714
(payload, when decoded = some long-stale data)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它是服务于这个近半个小时过去的 5分钟时间段; 它看起来像OutputCache的内部只是没有注意到时间; p它最终 …
我在测试网站上使用mvc-mini-profiler.当我在我的action方法上放置一个OutputCacheAttribute时,不会执行探查器并始终返回缓存之前的最后一个值.
有没有办法告诉mvc-mini-profiler结果来自缓存,以便它可以更新他的状态?也许在客户端网站上,我们可以看到这样的信息:
http://localhost/Home (from cache) 2.1ms, +0.5ms
Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于缓存的帖子,但它们中没有一个确实符合我的需求.在我的mvc 3应用程序中,我有一个动作方法GetImage(),它返回一个图像类型的文件.然后我在视图中使用此方法来显示图像:
<img width="75" height="75" src="@Url.Action("GetImage", "Store", new {productId = item.ProductId})"/>
Run Code Online (Sandbox Code Playgroud)
我想在服务器上缓存图像.所以,我已经尝试过:
1)使用OutputCacheAttribute:
[HttpGet, OutputCache(Duration = 10, VaryByParam = "productId", Location = OutputCacheLocation.Server, NoStore = true)]
public FileContentResult GetImage(int productId)
{
var p = _productRepository.GetProduct(productId);
if (p != null)
{
if (System.IO.File.Exists(GetFullProductImagePath(productId)))
{
var image = Image.FromFile(GetFullProductImagePath(productId));
return File(GetFileContents(image), "image/jpeg");
}
}
var defaultPath = AppDomain.CurrentDomain.BaseDirectory +
ConfigurationManager.AppSettings["default-images-directory"];
var defaultImage = Image.FromFile(Path.Combine(defaultPath, "DefaultProductImage.jpg"));
return File(GetFileContents(defaultImage), "image/jpeg");
}
Run Code Online (Sandbox Code Playgroud)
图像没有缓存(我得到状态:200 OK)
2)在GetImage()方法中使用以下Response.Cache方法:
public FileContentResult GetImage(int productId)
{
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我已经将ASP.NET Web API CacheOutput库用于我的web.net的asp.net项目并且它工作正常,但是我有一个POST方法的另一个控制器,我想从该控制器使我的缓存无效.
[AutoInvalidateCacheOutput]
public class EmployeeApiController : ApiController
{
[CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100)]
public IEnumerable<DropDown> GetData()
{
//Code here
}
}
public class EmployeesController : BaseController
{
[HttpPost]
public ActionResult CreateEmployee (EmployeeEntity empInfo)
{
//Code Here
}
}
Run Code Online (Sandbox Code Playgroud)
我希望在员工控制器中添加\ update时使Employees Cache无效.
outputcache ×10
asp.net-mvc ×6
asp.net ×5
caching ×3
.net ×1
action ×1
controller ×1
json ×1
substitution ×1