質問

一生、私が取り組んでいる MVC プロジェクトで SqlProfileProvider を動作させることはできません。

最初に気づいた興味深い点は、Visual Studio が ProfileCommon プロキシ クラスを自動的に生成しないということです。これは ProfileBase クラスを拡張するだけなので、大した問題ではありません。ProfileCommon クラスを作成した後、ユーザー プロファイルを作成するための次の Action メソッドを作成しました。

[AcceptVerbs("POST")]
public ActionResult CreateProfile(string company, string phone, string fax, string city, string state, string zip)
{
    MembershipUser user = Membership.GetUser();
    ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;

    profile.Company = company;
    profile.Phone = phone;
    profile.Fax = fax;
    profile.City = city;
    profile.State = state;
    profile.Zip = zip;
    profile.Save();

    return RedirectToAction("Index", "Account"); 
}

私が抱えている問題は、ProfileCommon.Create() への呼び出しが ProfileCommon 型にキャストできないため、プロファイル オブジェクトを取得できず、プロファイルが null であるため次の行が明らかに失敗することです。

以下は私の web.config のスニペットです。

<profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
    <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>
    <properties>
        <add name="FirstName" type="string" />
        <add name="LastName" type="string" />
        <add name="Company" type="string" />
        <add name="Phone" type="string" />
        <add name="Fax" type="string" />
        <add name="City" type="string" />
        <add name="State" type="string" />
        <add name="Zip" type="string" />
        <add name="Email" type="string" >
    </properties>
</profile>

MembershipProvider は問題なく動作しているため、接続文字列が適切であることがわかります。

役立つ場合に備えて、これが私の ProfileCommon クラスです。

public class ProfileCommon : ProfileBase
    {
        public virtual string Company
        {
            get
            {
                return ((string)(this.GetPropertyValue("Company")));
            }
            set
            {
                this.SetPropertyValue("Company", value);
            }
        }

        public virtual string Phone
        {
            get
            {
                return ((string)(this.GetPropertyValue("Phone")));
            }
            set
            {
                this.SetPropertyValue("Phone", value);
            }
        }

        public virtual string Fax
        {
            get
            {
                return ((string)(this.GetPropertyValue("Fax")));
            }
            set
            {
                this.SetPropertyValue("Fax", value);
            }
        }

        public virtual string City
        {
            get
            {
                return ((string)(this.GetPropertyValue("City")));
            }
            set
            {
                this.SetPropertyValue("City", value);
            }
        }

        public virtual string State
        {
            get
            {
                return ((string)(this.GetPropertyValue("State")));
            }
            set
            {
                this.SetPropertyValue("State", value);
            }
        }

        public virtual string Zip
        {
            get
            {
                return ((string)(this.GetPropertyValue("Zip")));
            }
            set
            {
                this.SetPropertyValue("Zip", value);
            }
        }

        public virtual ProfileCommon GetProfile(string username)
        {
            return ((ProfileCommon)(ProfileBase.Create(username)));
        }
    }

私が間違っていることについて何か考えはありますか?他のメンバーの中で、ProfileProvider を ASP.NET MVC プロジェクトに正常に統合した人はいますか?

よろしくお願いします...

役に立ちましたか?

解決

行う必要があるのは次のとおりです。

1) Web.config のセクションで、他の属性設定に加えて「inherits」属性を追加します。

<profile inherits="MySite.Models.ProfileCommon" defaultProvider="....

2) 全体を取り除く <properties> Web.config のセクション。これらはすでにカスタム ProfileCommon クラスで定義されており、前のステップでカスタム クラスから継承するように指示されているためです。

3) ProfileCommon.GetProfile() メソッドのコードを次のように変更します。

public virtual ProfileCommon GetProfile(string username)        
{            
     return Create(username) as ProfileCommon;      
}

お役に立てれば。

他のヒント

質問全体についてはわかりませんが、コードで気づいたことが 1 つあります。

ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;

(ProfileCommon) と ProfileCommon としての の両方は必要ありません。どちらもキャストを行いますが、() は例外をスローし、キャストできない場合は as は null を返します。

試す ウェブプロファイルビルダー. 。これは、web.config から WebProfile クラス (ProfileCommon に相当) を自動生成するビルド スクリプトです。

MVC ベータ版の web.config ファイルが間違っています。SqlProfileProvider は System.Web.Security ではなく System.Web.Profile にあります。これを変更すると、機能し始めるはずです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top