如何分配配置文件的值?

 2023-02-17    482  

问题描述

我不知道我缺少了什么,但我在web.config文件中添加了配置文件属性,但无法访问配置文件.项目在代码中或创建新配置文件.

推荐答案

我今天有同样的问题,并学到了很多.

如何分配配置文件的值?

Visual Studio中有两种项目 – “网站项目”和”Web应用程序项目”.由于对我来说是一个完整的谜语,Web应用程序项目不能使用配置文件.直接……从Web.config文件中没有神奇地为您生成强类型类.滚动你自己的.

MSDN中的示例代码假定您使用的是网站项目,并且他们告诉您只是将<profile>部分添加到Web.config和与Profile. 属性相接,但这在Web应用程序项目中不起作用.

您有两种选择来滚动自己的:

(1)使用 web配置文件builder .这是您添加到Visual Studio的自定义工具,它会自动生成您在Web.config中的定义中所需的配置文件对象.

我选择不这样做,因为我不希望我的代码依赖这个额外的工具来编译,这可能会在他们试图建立我的代码时为别人造成别人的问题而不意识到他们需要这个工具.

(2)制作自己的类,从ProfileBase派生来表示您的自定义配置文件.这比似乎更容易.这是一个非常简单的示例,它添加了”fullname”字符串配置文件字段:

在web.config:

<profile defaultProvider="SqlProvider" inherits="YourNamespace.AccountProfile">

<providers>
     <clear />
     <add name="SqlProvider"
          type="System.Web.Profile.SqlProfileProvider"
          connectionStringName="sqlServerMembership" />
</providers>

</profile>

在一个名为compuleprofile.cs:

中的文件中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;

namespace YourNamespace
{
    public class AccountProfile : ProfileBase
    {
        static public AccountProfile CurrentUser
        {
            get { return (AccountProfile)
                         (ProfileBase.Create(Membership.GetUser().UserName)); }
        }

        public string FullName
        {
            get { return ((string)(base["FullName"])); }
            set { base["FullName"] = value; Save(); }
        }

        // add additional properties here
    }
}

设置配置文件值:

AccountProfile.CurrentUser.FullName = "Snoopy";

获取个人资料值

string x = AccountProfile.CurrentUser.FullName;

其他推荐答案

Web应用程序项目仍然可以使用ProfileCommon对象,但仅在运行时使用.它的代码刚刚在项目本身中生成,但该类由ASP.NET生成,并且在运行时存在.

到达对象的最简单方法是使用如下所示的动态类型.

在web.config文件中声明配置文件属性:

<profile ...
 <properties>
   <add name="GivenName"/>
   <add name="Surname"/>
 </properties>

然后访问属性:

dynamic profile = ProfileBase.Create(Membership.GetUser().UserName);
string s = profile.GivenName;
profile.Surname = "Smith";

要保存更改配置文件属性:

profile.Save();

如果您使用动态类型感到舒适,以上工作正常,并不介意缺乏编译时间检查和智能is.

如果使用ASP.NET MVC使用此功能,如果将动态配置文件对象传递给您的视图,则必须执行一些额外的工作,因为HTML Helper方法不符合动态的”型号”对象.在将它们传递给HTML帮助程序方法之前,您必须将配置文件属性分配给静态键入的变量.

// model is of type dynamic and was passed in from the controller
@Html.TextBox("Surname", model.Surname) <-- this breaks

@{ string sn = model.Surname; }
@Html.TextBox("Surname", sn); <-- will work

如果您创建自定义配置文件类,作为上述joel,ASP.NET仍将生成ProfileCommon类,但它将继承自定义配置文件类.如果未指定自定义配置文件类,则Profilecommon将从System.Web.profile.profileBase继承.

如果创建自己的配置文件类,请确保未在您在自定义配置文件类中声明的web.config文件中指定配置文件属性.如果您在尝试生成ProfileCommon类时,ASP.NET将提供编译器错误.

其他推荐答案

配置文件也可以在Web应用程序项目中使用.
属性可以在Web.config中定义在设计时间或以编程方式中.在web.config中:

<profile enabled="true" automaticSaveEnabled="true" defaultProvider="AspNetSqlProfileProvider">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="TestRolesNProfiles"/>
      </providers>
      <properties>
        <add name="FirstName"/>
        <add name="LastName"/>
        <add name ="Street"/>
        <add name="Address2"/>
        <add name="City"/>
        <add name="ZIP"/>
        <add name="HomePhone"/>
        <add name="MobilePhone"/>
        <add name="DOB"/>

      </properties>
    </profile>

或以编程方式,通过实例化 profiles 和使用 profilepropertysettings创建单个属性来创建配置文件部分和 profilepropertysctingscolletion ,所有这些都在系统中.web.configuration命名空间.
要使用配置文件的这些属性,请使用system.web.profile.profilebase对象.无法使用配置文件访问配置文件属性.语法如上所述,但可以通过实例化profilebase并使用 setPropertyValue (” PropertyName 来轻松完成”)和 getPropertyValue {” propertyname “,如下所示:

ProfileBase curProfile = ProfileBase.Create("MyName");

或访问当前用户的配置文件:

ProfileBase curProfile = ProfileBase.Create(System.Web.Security.Membership.GetUser().UserName);



        curProfile.SetPropertyValue("FirstName", this.txtName.Text);
        curProfile.SetPropertyValue("LastName", this.txtLname.Text);
        curProfile.SetPropertyValue("Street", this.txtStreet.Text);
        curProfile.SetPropertyValue("Address2", this.txtAdd2.Text);
        curProfile.SetPropertyValue("ZIP", this.txtZip.Text);
        curProfile.SetPropertyValue("MobilePhone", txtMphone.Text);
        curProfile.SetPropertyValue("HomePhone", txtHphone.Text);
        curProfile.SetPropertyValue("DOB", txtDob.Text);
        curProfile.Save();

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

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

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

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