문제

저는 평생 동안 제가 작업 중인 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 유형으로 변환될 수 없기 때문에 프로필 객체를 다시 가져올 수 없다는 것입니다. 이로 인해 분명히 profile이 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;      
}

도움이 되었기를 바랍니다.

다른 팁

전체 질문에 대해서는 확실하지 않지만 코드에서 내가 발견한 한 가지는 다음과 같습니다.

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