开发者

Request.IsAuthenticated problem with Cache in ASP.NET

开发者 https://www.devze.com 2022-12-25 00:15 出处:网络
I\'m new in ASP.NET and I have a problem... When I want to cache I View or an Action like this : <%@ Page title=\"\" language=\"C#\" masterpagefile=\"~/Views/Shared/MemberHome.Master\" inherits=

I'm new in ASP.NET and I have a problem...

When I want to cache I View or an Action like this :

<%@ Page title="" language="C#" masterpagefile="~/Views/Shared/MemberHome.Master" inherits="System.Web.Mvc.ViewPage<IndexViewData>" %>
<%@ OutputCache duration="400" varybyparam="divId;regionId;page" %> 

I know that it cache all data in my page ... But in my page I have a condition like this :

 <% if(Request.IsAuthenticated) { %>
                <a href="/fr/Advertiser/Search"><img src="/content/images/v_2/bot.jpg" alt="Entreprises liées à vos passions" title="Entreprises liées à vos passions" /></a>
            <% } else { %>
                <a href="/fr/Advertiser/OpenSearch"><img src="/content/images/v_2/bot.jpg" alt="Entreprises liées à vos passions" title="Entreprises liées à vos passions" /></a>
            <% } %>    

I dont want to cache this variable : Request.IsAuthenticated ... because some result depend of this condition 开发者_运维知识库... I try the donut caching by scottgu's but it return (I think) just some text not a bool ... http://weblogs.asp.net/scottgu/archive/2006/11/28/tip-trick-implement-donut-caching-with-the-asp-net-2-0-output-cache-substitution-feature.aspx

Now I'm tired to try anything that come to my mind .. can you help me pleaseee! :)

Julien.


First off, in an MVC application, do not use the <%@ OutputCache %> directive on a view. Caching should be done at the controller level, not at the view level. Use the [OutputCache] attribute instead of <%@ OutputCache %>. See http://www.asp.net/learn/mvc/tutorial-15-cs.aspx for an example of how to do this.

You can set the OutputCacheAttribute.VaryByCustom property to some custom value and check it from Global.asax by overriding the GetVaryByCustomString method.

Steven Smith has an example of this on his blog. Note that his example targets WebForms instead of MVC, so you'd need to use [OutputCache] instead of <%@ OutputCache %>, but the general idea is the same. And you'll probably want to return "authenticated" or "unauthenticated" since that's all the granularity you need; it doesn't seem like you need the actual logged-in user name. (Output caching per-user is generally a bad idea anyway, since you'll quickly fill your cache if many users are logged in.)

Of particular note, do not use Response.WriteSubstitution() from within an MVC application. Response.WriteSubstitution() is a WebForms-specific extensibility point, and its behavior in an MVC application is undefined since MVC does not run the WebForms pipeline in its entirety. There is no guarantee that it will work properly.


By definition, you are caching the page and the resulting page will not be modified. So once you cache it with Request.IsAuthenticated == True, then it will continue to display that result and will not be regenerated until the cache expires or your other conditions you have present dictate for it to. I think what you need, if your caching it, is two different pages: one for authenticated users and one for unauthenticated users.

0

精彩评论

暂无评论...
验证码 换一张
取 消