Profile.GetProfile()
ライブラリクラスでメソッドを使用する方法がわかりません。Page.aspx.csでこのメソッドを使用してみましたが、完全に機能しました。
page.aspx.csで機能し、クラスライブラリで機能するメソッドを作成するにはどうすればよいですか。
Profile.GetProfile()
ライブラリクラスでメソッドを使用する方法がわかりません。Page.aspx.csでこのメソッドを使用してみましたが、完全に機能しました。
page.aspx.csで機能し、クラスライブラリで機能するメソッドを作成するにはどうすればよいですか。
ASP.NETでは、ProfileはHttpContext.Current.Profileプロパティへのフックであり、 System.Web.Profile.ProfileBaseから派生したProfileCommonタイプの動的に生成されたオブジェクトを返します。
ProfileCommonにはGetProfile(string username)メソッドが含まれているようですが、ASP.NETアプリケーションのコンパイル時にほとんどのProfileCommonクラスが動的に生成されるため、MSDNで公式に文書化されていることはありません(Visual Studioではインテリセンスで表示されません)(プロパティとメソッドの正確なリストは、web.configで「プロファイル」がどのように構成されているかによって異なります。GetProfile()はこのMSDNページで言及されているので、本物のようです。
おそらくライブラリクラスで、問題はweb.configからの構成情報が取得されていないことです。ライブラリクラスはWebアプリケーションを含むSolultionの一部ですか、それともライブラリだけで作業していますか?
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);
}
}
}