4

Profile.GetProfile()ライブラリクラスでメソッドを使用する方法がわかりません。Page.aspx.csでこのメソッドを使用してみましたが、完全に機能しました。

page.aspx.csで機能し、クラスライブラリで機能するメソッドを作成するにはどうすればよいですか。

4

3 に答える 3

2

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の一部ですか、それともライブラリだけで作業していますか?

于 2009-09-18T21:55:13.830 に答える
1

System.Web.dllクラスライブラリへの参照を追加してみましたか?

if (HttpContext.Current == null) 
{
    throw new Exception("HttpContext was not defined");
}
var profile = HttpContext.Current.Profile;
// Do something with the profile
于 2009-09-18T21:07:47.623 に答える
0

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);
                }
            }        
        }
于 2014-12-15T12:06:31.137 に答える