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

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

Implementing Profile Provider in ASP.NET MVC

为了我的一生,我无法让SqlProfileProvider在我正在处理的MVC项目中工作。

我意识到的第一个有趣的事情是Visual Studio不会自动为您生成ProfileCommon代理类。没什么大不了的,因为扩展ProfileBase类很简单。创建ProfileCommon类之后,我编写了以下Action方法来创建用户个人资料。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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的摘要:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
    <providers>
        <clear/>
       
    </providers>
    <properties>
       
       
       
       
       
       
       
       
       
    </properties>
</profile>

MembershipProvider工作顺利,所以我知道连接字符串很好。

以防万一,这是我的ProfileCommon类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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"属性:

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

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

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

1
2
3
4
public virtual ProfileCommon GetProfile(string username)        
{            
     return Create(username) as ProfileCommon;      
}

希望这可以帮助。


尝试使用Web Profile Builder。这是一个构建脚本,可以从web.config自动生成一个WebProfile类(与ProfileCommon等效)。


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

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

您不需要(ProfileCommon)和as ProfileCommon。它们都进行强制类型转换,但是()会引发异常,而如果不能进行强制类型转换,则as将返回null。


MVC Beta中的web.config文件错误。 SqlProfileProvider位于System.Web.Profile中,而不位于System.Web.Security中。更改它,它应该开始为您工作。


推荐阅读

    linux扩展文件命令?

    linux扩展文件命令?,工作,地址,系统,信息,命令,目录,管理,情况,文件,标准,Lin

    linux打包项目命令?

    linux打包项目命令?,项目,文件,命令,软件,数字,系统,名称,工具,目录,格式,Lin

    linux项目更新命令行?

    linux项目更新命令行?,工作,地址,系统,数据,信息,项目,标准,电脑,目录,命令,l

    linux更改扩展命令?

    linux更改扩展命令?,管理,地址,数据,工作,系统,在线,工具,命令,环境,单位,Lin

    linux查看扩展名命令?

    linux查看扩展名命令?,工作,系统,地址,信息,软件,文件,标准,电脑,命令,目录,L

    linux配置文件夹命令?

    linux配置文件夹命令?,地址,系统,工作,信息,命令,标准,服务,名称,位置,文件,l

    linux命令设置扩展屏?

    linux命令设置扩展屏?,工具,系统,电脑,技术,软件,装备,情况,工作,命令,显示

    linux命令创建项目组?

    linux命令创建项目组?,管理,密码,项目,命令,系统,位置,文件,用户组,用户,文

    linux磁盘扩展命令?

    linux磁盘扩展命令?,在线,信息,扩大,风险,磁盘,虚拟机,容量,分区,目录,链接,L

    复制项目的linux命令?

    复制项目的linux命令?,系统,项目,文件,命令,目录,源文件,语法,选项,目标,功

    linux命令项目部署?

    linux命令项目部署?,项目,服务,环境,软件,系统,代理,管理,统一,做好,业务,怎

    linux部署项目的命令?

    linux部署项目的命令?,项目,系统,设备,环境,工作,基础,传播,信息,标准,代码,L

    linux项目常用命令?

    linux项目常用命令?,工作,地址,系统,信息,管理,项目,命令,目录,标准,网络,lin

    linux项目更新命令?

    linux项目更新命令?,系统,信息,网络,地址,网站,通信,管理,时间,项目,命令,lin

    启动项目命令linux?

    启动项目命令linux?,工作,经理,产品,技术,地址,数字,系统,项目,命令,信息,怎

    linux中扩展命令缩写?

    linux中扩展命令缩写?,网络,命令,信息,工作,管理,暂停,基础,公司,半导体,时

    linux编译项目的命令?

    linux编译项目的命令?,系统,工作,地址,软件,工具,信息,代码,项目,命令,环境,l

    linux怎么改扩展命令?

    linux怎么改扩展命令?,网络,密码,情况,软件,通用,地址,中心,环境,分区,根目

    项目linux常用命令?

    项目linux常用命令?,工作,地址,系统,管理,设备,信息,项目,命令,目录,标准,lin

    linux上部署项目命令?

    linux上部署项目命令?,项目,软件,服务,工具,环境,代理,地址,代码,首页,做好,l