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

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

有帮助吗?

解决方案

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

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

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

其他提示

你试过将参考System.Web.dll你的类库,然后:

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

您可以使用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);
                }
            }        
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top