web-development-kb-asia.site

Triển khai nhà cung cấp hồ sơ trong ASP.NET MVC

Đối với cuộc sống của tôi, tôi không thể để SqlProfileProvider hoạt động trong một dự án MVC mà tôi đang làm việc.

Điều thú vị đầu tiên mà tôi nhận ra là Visual Studio không tự động tạo lớp proxy ProfileCommon cho bạn. Đó không phải là vấn đề lớn vì đó là vấn đề mở rộng lớp ProfileBase. Sau khi tạo lớp ProfileCommon, tôi đã viết phương thức Action sau để tạo hồ sơ người dùng.

[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"); 
}

Vấn đề mà tôi gặp phải là cuộc gọi đến ProfileCommon.Create () không thể chuyển sang gõ ProfileCommon, vì vậy tôi không thể lấy lại đối tượng hồ sơ của mình, điều này rõ ràng khiến dòng tiếp theo không thành công vì hồ sơ là null.

Sau đây là một đoạn của web.config của tôi:

<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>

MemberhipProvider đang hoạt động mà không gặp trở ngại nào, vì vậy tôi biết rằng chuỗi kết nối là tốt.

Chỉ trong trường hợp nó hữu ích, đây là lớp ProfileCommon của tôi:

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)));
        }
    }

Bất kỳ suy nghĩ về những gì tôi có thể làm sai? Có ai trong số các bạn còn lại đã tích hợp thành công ProfileProvider với các dự án ASP.NET MVC của bạn không?

Cảm ơn bạn trước ...

63
senfo

Đây là những gì bạn cần làm:

1) Trong phần của Web.config, thêm thuộc tính "kế thừa" bên cạnh các cài đặt thuộc tính khác của bạn:

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

2) Xóa toàn bộ <properties> phần từ Web.config, vì bạn đã xác định chúng trong lớp ProfileCommon tùy chỉnh của mình và cũng được hướng dẫn kế thừa từ lớp tùy chỉnh của bạn trong bước trước

3) Thay đổi mã của phương thức ProfileCommon.GetProfile () của bạn thành

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

Hi vọng điêu nay co ich.

56
Yogesh Prabhu

Hãy thử Trình tạo hồ sơ web . Đó là tập lệnh xây dựng tự động tạo lớp WebProfile (tương đương với ProfileCommon) từ web.config.

6
Dave Dunkin

Không chắc chắn về toàn bộ câu hỏi, nhưng một điều tôi nhận thấy trong mã của bạn:

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

Bạn không cần cả (ProfileCommon) và như ProfileCommon. Cả hai đều thực hiện phôi, nhưng () ném và ngoại lệ trong khi as trả về null nếu diễn viên không thể thực hiện.

6
Mladen Mihajlovic

Tệp web.config trong MVC Beta là sai. SqlProfileProvider nằm trong System.Web.Profile, không phải System.Web.Security. Thay đổi điều này, và nó sẽ bắt đầu làm việc cho bạn.

1
Steve Andrews