如何在库类中使用 Profile.GetProfile() ?

发布于 2024-08-05 08:21:58 字数 136 浏览 5 评论 0原文

我不知道如何在库类中使用 Profile.GetProfile() 方法。 我尝试在 Page.aspx.cs 中使用此方法,效果非常好。

我怎样才能制作一个在 page.aspx.cs 中工作的方法,在类库中工作。

I cant figure out how to use Profile.GetProfile() method in a library class.
I tried using this method in a Page.aspx.cs and it worked perfectly.

How can I make a method that works in the page.aspx.cs, work in the class library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国际总奸 2024-08-12 08:21:58

在 ASP.NET 中,Profile 是 HttpContext 的挂钩.Current.Profile 属性,返回动态生成的 ProfileCommon 类型的对象,派生自 System.Web.Profile.ProfileBase

ProfileCommon 显然包含一个 GetProfile(string username) 方法,但您不会在 MSDN 中找到它的正式记录(并且它不会显示在 Visual Studio 的智能感知中),因为大多数 ProfileCommon 类是在编译 ASP.NET 应用程序时动态生成的(属性和方法的确切列表将取决于 web.config 中“配置文件”的配置方式)。 GetProfile() 确实在此 MSDN 上得到提及页,所以它看起来是真实的。

也许在您的库类中,问题是没有获取来自 web.config 的配置信息。您的库类是包含 Web 应用程序的解决方案的一部分,还是您只是单独处理该库?

In ASP.NET, Profile is a hook into the HttpContext.Current.Profile property, which returns a dynamically generated object of type ProfileCommon, derived from System.Web.Profile.ProfileBase.

ProfileCommon apparently includes a GetProfile(string username) method, but you wont find it documented officially in MSDN (and it wont show up in intellisense in visual studio) because most of the ProfileCommon class is dynamically generated when your ASP.NET application is compiled (The exact list of properties and methods will depend on how 'profiles' are configured in your web.config). GetProfile() does get a mention on this MSDN page, so it seems to be real.

Perhaps in your library class, the problem is that the configuration info from web.config is not being picked up. Is your library class part of a Solultion that includes a Web Application, or are you just working on the library in isolation?

日暮斜阳 2024-08-12 08:21:58

您是否尝试将对 System.Web.dll 的引用添加到您的类库中,然后:

if (HttpContext.Current == null) 
{
    throw new Exception("HttpContext was not defined");
}
var profile = HttpContext.Current.Profile;
// Do something with the profile

Have you tried adding reference to System.Web.dll to your class library and then:

if (HttpContext.Current == null) 
{
    throw new Exception("HttpContext was not defined");
}
var profile = HttpContext.Current.Profile;
// Do something with the profile
心不设防 2024-08-12 08:21:58

您可以使用 ProfileBase,但会失去类型安全性。您可以通过仔细的转换和错误处理来缓解这种情况。

    string user = "Steve"; // The username you are trying to get the profile for.
    bool isAuthenticated = false;

        MembershipUser mu = Membership.GetUser(user);

        if (mu != null)
        {
            // User exists - Try to load profile 

            ProfileBase pb = ProfileBase.Create(user, isAuthenticated);

            if (pb != null)
            {
                // Profile loaded - Try to access profile data element.
                // ProfileBase stores data as objects in a Dictionary 
                // so you have to cast and check that the cast succeeds.

                string myData = (string)pb["MyKey"];

                if (!string.IsNullOrWhiteSpace(myData))            
                {
                    // Woo-hoo - We're in data city, baby!
                    Console.WriteLine("Is this your card? " + myData);
                }
            }        
        }

You can use ProfileBase, but you lose type-safety. You can mitigate that with careful casting and error handling.

    string user = "Steve"; // The username you are trying to get the profile for.
    bool isAuthenticated = false;

        MembershipUser mu = Membership.GetUser(user);

        if (mu != null)
        {
            // User exists - Try to load profile 

            ProfileBase pb = ProfileBase.Create(user, isAuthenticated);

            if (pb != null)
            {
                // Profile loaded - Try to access profile data element.
                // ProfileBase stores data as objects in a Dictionary 
                // so you have to cast and check that the cast succeeds.

                string myData = (string)pb["MyKey"];

                if (!string.IsNullOrWhiteSpace(myData))            
                {
                    // Woo-hoo - We're in data city, baby!
                    Console.WriteLine("Is this your card? " + myData);
                }
            }        
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文