天天看點

如何在Web App Project 或者 Web Site Project的App_Code 内使用 Profile/ProfileCommon

在 web site project 内 可以很友善的使用 Profile/ProfileCommon 來 通路我們在web.config 的profile節内定義的properties , 并且有很爽的 intellisense

然而在Web App Project或者Web Site Project的App_Code内使用的時候 編譯都通不過的,因為 Profile 是 web site project 模型 在頁面 執行時候建立在HttpContext的,Web Site Project 或者App_Code編譯的時候還沒有頁面執行個體呢,何談HttpContext, 當然無法使用了

引用scottgu的原話解釋“This is supported because with the VS 2005 Web Site Project option Visual Studio is dynamically creating and adding a "ProfileCommon" class named "Profile" into every code-behind instance”,那麼怎麼辦呢 scottgu 支招------“VS 2005 Web Application Projects don't automatically support generating a strongly-typed Profile class proxy. However, you can use this free download to automatically keep your own proxy class in sync with the profile configuration”。

我們可以到 gotdotnet 上下載下傳這個 addin  安裝,經我測試 這個東西裝在中文VS2005 SP1 上右鍵不會出現菜單(隻會出現在外部程式管理器内,看的到用不成,不開心)。

好在他有源碼的,我們用他的源碼 找到 大概 40行上下

string toolsMenuName ;定義部分

直接 string toolsMenuName = "工具"; 然後下面的 try catch 注釋掉----編譯---然後把dll檔案

替換到 C:\Documents and Settings\武眉博\My Documents\Visual Studio 2005\AddIns

就可以用了(原因可能是他的資源檔案有問題)

接下來按照readme使用吧。

==================================================

To use the generator right click on web.config in a Web Application Project and

select "Generate WebProfile."  This will create a WebProfile class in your

project based on the current profile setting sin Web.Config.  If you make a

change to your profile setting you need to run the tool again to update the

WebProfile class.  The WebProfile class is simply a thin wrapper that has

strongly typed accessors to profile properties.

To use the web profile class in a page create an accessor like this:

    // C# accessor

    private WebProfile Profile

    {

        get { return new WebProfile(Context.Profile); }

    }

    ' VB accessor

    Private ReadOnly Property Profile() As WebProfile

        Get

            Return New WebProfile(Context.Profile)

        End Get

    End Property

Then you can use it like this:

    // C# use or accessor

    string s = Profile.MyProperty;

    Profile.MyGroup.MyProperty = "value";

    ' VB use of accessor

    Dim prop As String = Profile.MyProperty

    Profile.MyGroup.MyProperty = "value"

You can also access the current profile using the static Current property

like this:

    // C# use of Current property

    string s = WebProfile.Current.MyProperty;

    WebProfile.Current.MyGroup.MyProperty = "value";

    // VB use of Current property

    Dim s As String = WebProfile.Current.MyProperty

    WebProfile.Current.MyGroup.MyProperty = "value"

    ========================================================

如果你用的是Web Site Project 想在App_Code内用Profile那麼建議 建立一個WebSiteProject web.config拷貝過去仍然使用這個AddIn生成一個WebProfile.cs拷貝回你的app_code内就可以了(哎,怎麼感覺像是說了個廢話,還不如直接用Web App Project呢)

OK,就這麼幾步了。記在這裡希望對朋友們有用,睡覺了。

參考資料: http://webproject.scottgu.com/CSharp/migration2/migration2.aspx

轉載于:https://www.cnblogs.com/huobazi/archive/2007/06/06/HowToUserProfileInWebSiteProjectInVs2005Sp1.html

c#