这是已有的问题:在我这里还没有解决,把它贴在这里,希望知道的朋友指点,原问题页面 http://forums.asp.net/t/1161751.aspx

Hi,

Two days ago someone asked me if I can solve a problem he is experiencing with page that has OutputCache enabled and he wants to use some values stored in the HTTP Session. His problem is every time he accesses the page he gets the first data which was stored in the session even though the session state has been changed. As far as I know this can be solved by two options

  1. Refactor the page with user controls (Partial Caching) and set the cache on the portion he needs.
  2. Use Substution control.
I have chosen to solve this using Substition control.  so in the page I set
<asp:Substitution ID="Substitution1" runat="server" MethodName="getUserName" />
 Then I declared the static method as
public static string getUserName( HttpContext context)
    {
        
return (string)context.Session["userName"];
    }
 

When I test the page at the first time it displays the content correctly, but if I refresh the page or call it at the second time I get null pointer exception saying Session object inside the context is nullable

if I remove the caching it works fine.

<%@ OutputCache Duration="60"  VaryByParam="none" %>
 I did find this but no one answered

 Is there something I did wrong or is this bug?

 

Thanks

/////////////////////////////////////////////////////////////////////////

我就纳闷的很了,只要refresh 页面,就会报这个错误

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:

行 231:
            行 232:        ////HttpContext.Current.Session.RemoveAll();
            行 233:        if (context.Session["UserName"] != null)
            行 234:        {
            行 235:

调试了很长时间了,真想把页面分为几个用户自定义控件了,希望能得到知道的朋友指点

--------------------------------------------------------------------------------

有人建议用其他方式取session

1.HttpContext.Current.Request.Cookies("MyCookie")("ValueName")

and it works fine.//我没有测试

2.

I don't understand why this has been marked as answered as I'm pretty sure that none of the posts solve the problem. When the method for the Substitution control is first called i.e. when the page hasn't been cached session & cookies are available. BUT on subsequent calls when the cached page is being used the context.session = null (as mentioned), but also though context.Response.Cookies is populated with the cookie keys, ALL the values are unset so this cannot be used either. This is when continually requesting the same page (not a postback).

Having implemented the substitution control to use session and discovering it wouldn't work, this post led me to believe that using cookies would solve the problem, but it doesn't. This leads me on to conclude that substition controls aren't much use if you cannot access any user data to populate them with (apart from putting in the time or date). 

You can use Profile data instead of session, which is accessable in the Substition method using eg:- 

public static String GetRealName(HttpContext context)
{    ProfileBase profile 
= ProfileBase.Create(context.User.Identity.Name);  
  realName 
= profile.GetPropertyValue("realname").ToString();  
  
return realName;
}

However the GetPropertyValue call makes a hit to SQL server to get the value everytime so the gain of page caching might well be lost in hitting sql server on every page request. I am now thinking that I am better off reverting to just using caching for my datasources and turning off whole page caching as though it's a good idea in theory, in practice the implementation seems too inflexible.

 
UPDATE: After many hours experimentation I have finally found a way to get cookies to work with the Substitution control, it's a bit weird, so I'll test it further and suss out why it works before posting, too late tonight though...