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

 2023-02-17    356  

问题描述

在我的生活中,我无法让SQLProfileProvider在我正在研究的MVC项目中工作.

我意识到的第一个有趣的事情是Visual Studio不会为您自动生成ProfileCommon Proxy类.这不是一个大的交易,因为它正在延伸剖面牌类的问题.创建ProfileCommon类后,我编写了以下操作方法来创建用户配置文件.

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

[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,因此我无法恢复我的个人资料对象,这显然会导致下一行失败以来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>

empersepprovider在没有挂钩的情况下工作,所以我知道连接字符串很好.

以防我有用,这是我的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)));
        }
    }

对我可能做错什么的任何想法?您的其余部分都与您的ASP.NET MVC项目成功集成了ProfileProvider?

谢谢你提前…

推荐答案

这是您需要做的:

1)在Web.config的部分中,除了您的其他属性设置外,添加”继承”属性:

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

其他推荐答案

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

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

您不需要(ProfileCommon)和AS ProfileCommon.他们都做到了,但()抛出和例外,而如果不能制作铸造,则返回null.

其他推荐答案

try web profile builder .它是一个构建脚本,自动生成来自web.config的webprofile类(相当于profilecommon).

以上所述是小编给大家介绍的在ASP.NET MVC中实现配置文件提供者,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34253.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。