為了我的一生,我無法讓SqlProfileProvider在我正在處理的MVC項目中工作。我意識到的第一個有趣的事情是Visual Studio不會自動為您生成ProfileCommon代理類。沒什么大不了的,因為擴展ProfileBase類很簡單。創(chuàng)建ProfileCommon類之后,我編寫了以下Action方法來創(chuà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"); }我遇到的問題是對ProfileCommon.Create()的調(diào)用無法轉(zhuǎn)換為ProfileCommon類型,因此我無法取回我的配置文件對象,這顯然會導(dǎo)致下一行失敗,因為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工作順利,所以我知道連接字符串很好
3 回答

海綿寶寶撒
TA貢獻1809條經(jīng)驗 獲得超8個贊
嘗試使用Web Profile Builder。它是一個構(gòu)建腳本,可以從web.config自動生成一個WebProfile類(等效于ProfileCommon)。

侃侃爾雅
TA貢獻1801條經(jīng)驗 獲得超16個贊
不確定整個問題,但是我在您的代碼中注意到了一件事:
ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
您不需要(ProfileCommon)和as ProfileCommon。它們都執(zhí)行強制類型轉(zhuǎn)換,但是()引發(fā)異常,而as如果無法進行強制類型轉(zhuǎn)換,則返回null。
- 3 回答
- 0 關(guān)注
- 538 瀏覽
添加回答
舉報
0/150
提交
取消