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

來源:互聯網
上載者:User

在 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.