C# 如何在web应用程序中使用配置文件

C# 如何在web应用程序中使用配置文件,c#,asp.net,web-applications,C#,Asp.net,Web Applications,我从网站转到web应用程序来改进它,我的网站上有我的个人资料 我的配置文件不起作用,所以我实现System.Web.ProfileProvider并创建一个类ProfileCommon inherit ProfileBase。我使用HttpContext.Current.profile.SetPropertyValue和GetPropertyValue。它可以工作,但问题是它有一个逻辑错误,因为HttpContext.Current.Profile没有调用我的ProfileProvider方法。

我从网站转到web应用程序来改进它,我的网站上有我的个人资料

我的配置文件不起作用,所以我实现System.Web.ProfileProvider并创建一个类ProfileCommon inherit ProfileBase。我使用HttpContext.Current.profile.SetPropertyValue和GetPropertyValue。它可以工作,但问题是它有一个逻辑错误,因为HttpContext.Current.Profile没有调用我的ProfileProvider方法。我如何修复它?或者,在web应用程序中是否有其他配置文件实现

    public class ProfileCommon : ProfileBase
{
    public string FirstName 
    {
        get
        {
            return HttpContext.Current.Profile.GetPropertyValue("FirstName").ToString();
        }
        set
        {
            HttpContext.Current.Profile.SetPropertyValue("FirstName", value);
        }
    }
}
在web.config中

<profile inherits="BaniBookWebApp.Code.Core.ProfileCommon">
  <providers>
    <add name="CustomProfileProvider" type="BaniBookWebApp.Code.Core.CustomProfileProvider"/> 
  </providers>     
</profile>

CustomProfileProvider:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eLearn2Model;
using System.Collections;
using System.Configuration;

namespace AccessControl
{
    public class ProfileProvider : System.Web.Profile.ProfileProvider
    {
        public ProfileProvider()
        {
        }

        public override int DeleteInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            int res = -1;
            using (var db = new eLearn2Entities())
            {
                List<Profile> profiles = (from m in db.Profiles
                                                 where (m.LastUpdatedDate.CompareTo(userInactiveSinceDate) < 0)
                                                 select m).ToList();
                foreach (Profile profile in profiles)
                {
                    if (profile != null)
                    {
                        db.Profiles.DeleteObject(profile);
                        res = db.SaveChanges();
                    }
                }
            }
            return res;
        }

        public override int DeleteProfiles(string[] usernames)
        {
            int res = -1;
            using (var db = new eLearn2Entities())
            {
                foreach (string username in usernames)
                {
                    Profile profile = (from m in db.Profiles
                                              join n in db.Users on m.UserId equals n.UserId
                                              where n.UserName == username
                                              select m).SingleOrDefault();
                    if (profile != null)
                    {
                        db.Profiles.DeleteObject(profile);
                        res = db.SaveChanges();
                    }
                }
            }
            return res;
        }

        public override int DeleteProfiles(System.Web.Profile.ProfileInfoCollection profiles)
        {
            throw new NotImplementedException();
        }      

        public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
        {
            System.Configuration.SettingsPropertyValueCollection spvc = new System.Configuration.SettingsPropertyValueCollection();
            lock (collection.SyncRoot)
            {
                foreach (object item in collection)
                {
                    SettingsProperty sp = (SettingsProperty)item;
                    SettingsPropertyValue spv = new SettingsPropertyValue(sp);
                    spvc.Add(spv);
                }
            }
            return spvc;
        }

        public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
        {
            string PropertyNames_ = string.Empty;
            string PropertyValuesString_ = string.Empty;
            string userID = string.Empty;
            lock (collection.SyncRoot)
            {
                foreach (object item in collection)
                {
                    SettingsPropertyValue spv = (SettingsPropertyValue)item;
                    if (spv.PropertyValue != null)
                    {
                        if (!string.IsNullOrEmpty(spv.PropertyValue.ToString()))
                        {
                            if (spv.Name.Equals("UserID"))
                            {
                                userID = spv.PropertyValue.ToString();
                            }
                            else
                            {
                                PropertyNames_ += spv.Name + ";";
                                PropertyValuesString_ += spv.PropertyValue.ToString() + ";";
                            }
                        }
                    }
                }//foreach            
            }//lock

            try
            {
                using (var db = new eLearn2Entities())
                {
                    bool isAuthenticated = bool.Parse(context["IsAuthenticated"].ToString());

                    Profile profile = new Profile()
                    {
                        UserId = Guid.Parse(userID),                       
                        PropertyValuesString = PropertyValuesString_,
                        PropertyNames = PropertyNames_,
                        LastUpdatedDate = DateTime.UtcNow
                    };
                    db.Profiles.AddObject(profile);
                    db.SaveChanges();
                }
            }
            catch
            {
                using (var db = new eLearn2Entities())
                {
                    //bookmark gives me an error said multiple record
                    Guid myID = Guid.Parse(userID);
                    Profile existed_profile = db.Profiles.Where
                        (item => item.UserId == myID).SingleOrDefault() as Profile;

                    existed_profile.PropertyValuesString = PropertyValuesString_;
                    existed_profile.PropertyNames = PropertyNames_;
                    existed_profile.LastUpdatedDate = DateTime.UtcNow;

                    db.Profiles.ApplyOriginalValues(existed_profile);
                    db.SaveChanges();
                }

            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用eLearn2模型;
使用系统集合;
使用系统配置;
命名空间访问控制
{
公共类ProfileProvider:System.Web.Profile.ProfileProvider
{
公共档案提供者()
{
}
public override int DeleteInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption,DateTime userInactiveSinceDate)
{
int res=-1;
使用(var db=新eLearn2Entities())
{
列表配置文件=(从db.profiles中的m开始)
其中(m.lastUpdateDate.CompareTo(userInactivateSinceDate)<0)
选择m.ToList();
foreach(配置文件中的配置文件)
{
if(profile!=null)
{
db.Profiles.DeleteObject(profile);
res=db.SaveChanges();
}
}
}
返回res;
}
公共覆盖int DeleteProfiles(字符串[]用户名)
{
int res=-1;
使用(var db=new eLearn2Entities())
{
foreach(用户名中的字符串用户名)
{
Profile Profile=(从数据库Profiles中的m开始)
在数据库中加入n。m.UserId上的用户等于n.UserId
其中n.UserName==用户名
选择m).SingleOrDefault();
if(profile!=null)
{
db.Profiles.DeleteObject(profile);
res=db.SaveChanges();
}
}
}
返回res;
}
公共覆盖int DeleteProfiles(System.Web.Profile.ProfileInfoCollection配置文件)
{
抛出新的NotImplementedException();
}      
公共重写System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext上下文,System.Configuration.SettingsPropertyCollection集合)
{
System.Configuration.SettingsPropertyValueCollection spvc=新的System.Configuration.SettingsPropertyValueCollection();
锁(collection.SyncRoot)
{
foreach(集合中的对象项)
{
设置属性sp=(设置属性)项;
SettingsPropertyValue spv=新的SettingsPropertyValue(sp);
新增特殊目的公司(spv);
}
}
返回spvc;
}
公共重写无效SetPropertyValue(System.Configuration.SettingsContext上下文,System.Configuration.SettingsPropertyValueCollection集合)
{
string PropertyNames_uz=string.Empty;
string PropertyValuesString_u2;=string.Empty;
string userID=string.Empty;
锁(collection.SyncRoot)
{
foreach(集合中的对象项)
{
SettingsPropertyValue spv=(SettingsPropertyValue)项;
if(spv.PropertyValue!=null)
{
如果(!string.IsNullOrEmpty(spv.PropertyValue.ToString()))
{
if(spv.Name.Equals(“UserID”))
{
userID=spv.PropertyValue.ToString();
}
其他的
{
PropertyNames_uz+=spv.Name+“;”;
PropertyValuesString_+=spv.PropertyValue.ToString()+“;”;
}
}
}
}//弗雷奇
}//锁
尝试
{
使用(var db=new eLearn2Entities())
{
bool isAuthenticated=bool.Parse(上下文[“isAuthenticated”].ToString());
配置文件=新配置文件()
{
UserId=Guid.Parse(UserId),
PropertyValuesString=PropertyValuesString,
PropertyNames=PropertyNames_389;,
LastUpdateDate=DateTime.UtcNow
};
db.Profiles.AddObject(profile);
db.SaveChanges();
}
}
抓住
{
使用(var db=new eLearn2Entities())
{
//书签给了我一个错误,说是多条记录
Guid myID=Guid.Parse(userID);
Profile existed\u Profile=db.Profiles.Where
(item=>item.UserId==myID);
已存在的\u profile.PropertyValuesString=PropertyValuesString;
已存在的\u profile.PropertyNames=PropertyNames;
已存在\u profile.lastUpdateDate=DateTime.UtcNow;
<profile inherits="BaniBookWebApp.Code.Core.ProfileCommon" enabled="true">