我不知道如何Profile.GetProfile()
在库类中使用方法。我尝试在 Page.aspx.cs 中使用此方法,并且效果很好。
如何使在 page.aspx.cs 中有效的方法在类库中有效。
我不知道如何Profile.GetProfile()
在库类中使用方法。我尝试在 Page.aspx.cs 中使用此方法,并且效果很好。
如何使在 page.aspx.cs 中有效的方法在类库中有效。
在 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 应用程序的解决方案的一部分,还是您只是孤立地处理库?
您是否尝试过添加对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);
}
}
}