首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在ASP.NET MVC中实现配置文件提供程序

在ASP.NET MVC中实现配置文件提供程序
EN

Stack Overflow用户
提问于 2008-09-17 09:56:37
回答 4查看 38.2K关注 0票数 63

在我的生命中,我无法让SqlProfileProvider在我正在工作的MVC项目中工作。

我意识到的第一件有趣的事情是Visual Studio不会自动为您生成ProfileCommon代理类。这不是什么大问题,因为扩展ProfileBase类很简单。在创建ProfileCommon类之后,我编写了以下操作方法来创建用户配置文件。

[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对象,这显然会导致下一行失败,因为profile为空。

以下是我的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项目集成在一起?

先谢谢你……

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-01-12 08:25:21

下面是你需要做的:

1)在Web.config的部分中,除了您的其他属性设置之外,还需要添加"inherits“属性:

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

2)从Web.config中删除整个<properties>部分,因为您已经在自定义ProfileCommon类中定义了它们,并且在上一步中还被指示继承自定义类

3)将ProfileCommon.GetProfile()方法的代码更改为

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

希望这能有所帮助。

票数 56
EN

Stack Overflow用户

发布于 2008-09-17 06:45:14

不确定整个问题,但我在您的代码中注意到一件事:

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

您不需要同时使用( ProfileCommon )和as ProfileCommon。它们都进行强制转换,但()抛出异常,而如果无法进行强制转换,则as返回null。

票数 6
EN

Stack Overflow用户

发布于 2008-09-18 16:06:43

试试Web Profile Builder。它是一个构建脚本,可以从web.config自动生成一个WebProfile类(相当于ProfileCommon)。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/79129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档