c# - 我如何在 Web 应用程序中使用配置文件

标签 c# asp.net web-applications

我从网站转移到 Web 应用程序以改进它,我的网站上有个人资料。

我的配置文件不起作用,所以我实现了 System.Web.ProfileProvider 并创建了一个类 ProfileCommon 继承 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>

自定义配置文件提供者:

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

            }
        }
    }
}

最佳答案

您是否尝试过在 web.config 中添加 enabled="true"?

<profile inherits="BaniBookWebApp.Code.Core.ProfileCommon" enabled="true">

关于c# - 我如何在 Web 应用程序中使用配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8040603/

相关文章:

c# - 为什么将类移动到 Visual Studio 中的新文件夹中会造成破坏?

javascript - Bot Framework 版本 4 : changes in botchat. cs、styles.css 和 botchat.js 与版本 3 相比

c# - 将 IDictionary<string, string> 键转换为小写 (C#)

php - 带有 IIS 的 ASP.NET VS 带有 Apache 的 PHP

html - 通过URL生成的托管图像(特别是QR码)

c# - 从具体化 'System.Int32' 类型到 'System.String' 类型的指定转换无效

c# - x 分钟后 Microsoft Azure 运行功能

c# - 如何使用 ASP.NET 跟踪下载?

web-applications - 用于大型应用程序的backbone.js

web-applications - Web 应用程序中的客户 Google Analytics