【问题标题】:Property losing value even though declared Public即使宣布为公共财产,财产也会贬值
【发布时间】:2014-03-14 16:22:23
【问题描述】:

我在App_code 中有一个文件SiteMinder.CS,我在其中设置了访问该网页的UserID

public class SiteMinder : IHttpModule
    {
        public string UserID { get; set; }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandler);
        }

        private void Application_PreRequestHandler(Object source, EventArgs e)
        {
            if (HttpContext.Current.Request.Headers != null)
            {
                NameValueCollection coll = HttpContext.Current.Request.Headers;
                UserID = coll["uid"]; // Doesn't have NULL value
            }
        }
    }

在另一个网页UserDetails.aspx.cs 文件中,我尝试访问此UserID,但它具有NULL 值。

public partial class UserDetails : System.Web.UI.Page
{

    protected string SessionUser { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMinder objSite = new SiteMinder();
        SessionUser = objSite.UserID;//Returns NULL
    }
}

所有这些都在同一个命名空间下。请让我知道我在哪里错了。

【问题讨论】:

  • 需要调用init和Application_PreRequest来初始化UserID。目前你没有给他们打电话。

标签: c# asp.net properties encapsulation


【解决方案1】:

您正在创建一个 SiteMinder 对象。这与设置了属性的对象不同,因此该属性将具有默认值 (null)。

您需要获取对设置属性的原始SiteMinder 对象的引用 - 或将值存储在其他位置(例如HttpContext)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2011-03-13
    • 2011-12-21
    • 2019-08-28
    • 1970-01-01
    • 2016-02-13
    • 2018-05-18
    • 2010-10-15
    相关资源
    最近更新 更多