zsh*_*arp 110 asp.net profile asp.net-mvc asp.net-membership
我不知道我缺少什么,但我在Web.config文件中添加了配置文件属性,但无法访问配置文件.代码中的项目或创建新的配置文件.
Joe*_*sky 178
我今天遇到了同样的问题,并且学到了很多东西.
Visual Studio中有两种项目 - "Web站点项目"和"Web应用程序项目".出于对我来说完全神秘的原因,Web应用程序项目无法使用Profile.直接...强类型类不是从Web.config文件中为你神奇地生成的,所以你必须自己动手.
MSDN中的示例代码假定您正在使用Web站点项目,并且它们告诉您只是<profile>向您的Web.config和派对添加一个部分Profile.属性,但这在Web应用程序项目中不起作用.
您有两种选择可以自己动手:
(1)使用Web Profile Builder.这是您添加到Visual Studio的自定义工具,可以从Web.config中的定义自动生成所需的Profile对象.
我选择不这样做,因为我不希望我的代码依赖于这个额外的工具来编译,当他们试图构建我的代码而没有意识到他们需要这个工具时,这可能会给其他人带来问题.
(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>
Run Code Online (Sandbox Code Playgroud)
在名为AccountProfile.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
}
}
Run Code Online (Sandbox Code Playgroud)
要设置配置文件值:
AccountProfile.CurrentUser.FullName = "Snoopy";
Run Code Online (Sandbox Code Playgroud)
获取个人资料值
string x = AccountProfile.CurrentUser.FullName;
Run Code Online (Sandbox Code Playgroud)
col*_*ier 17
Web应用程序项目仍然可以使用ProfileCommon对象,但仅限于运行时.它的代码不是在项目本身生成的,而是由ASP.Net生成的类,并且在运行时存在.
获取对象的最简单方法是使用动态类型,如下所示.
在Web.config文件中声明配置文件属性:
<profile ...
<properties>
<add name="GivenName"/>
<add name="Surname"/>
</properties>
Run Code Online (Sandbox Code Playgroud)
然后访问属性:
dynamic profile = ProfileBase.Create(Membership.GetUser().UserName);
string s = profile.GivenName;
profile.Surname = "Smith";
Run Code Online (Sandbox Code Playgroud)
要保存对配置文件属性的更改:
profile.Save();
Run Code Online (Sandbox Code Playgroud)
如果您习惯使用动态类型并且不介意缺少编译时检查和智能感知,则上述工作正常.
如果将此与ASP.Net MVC一起使用,如果将动态配置文件对象传递给视图,则必须执行一些额外的工作,因为HTML帮助程序方法不能很好地与动态的"模型"对象一起使用.在将配置文件属性传递给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
Run Code Online (Sandbox Code Playgroud)
如果您创建自定义配置文件类,如上所述Joel,ASP.Net仍将生成ProfileCommon类,但它将继承自您的自定义配置文件类.如果未指定自定义配置文件类,则ProfileCommon将从System.Web.Profile.ProfileBase继承.
如果您创建自己的配置文件类,请确保未在自定义配置文件类中已声明的Web.config文件中指定配置文件属性.如果你这样做,ASP.Net会在尝试生成ProfileCommon类时给出编译器错误.
Bed*_*ara 13
配置文件也可以在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>
Run Code Online (Sandbox Code Playgroud)
或者以编程方式,通过实例化ProfileSection并使用ProfilePropertySettings和ProfilePropertySettingsColletion创建单个属性来创建配置文件部分,所有这些属性都在System.Web.Configuration命名空间中.要使用配置文件的这些属性,请使用System.Web.Profile.ProfileBase对象.无法使用配置文件访问配置文件属性.如上所述的语法,但可以通过实例化ProfileBase并使用SetPropertyValue(" PropertyName ")和GetPropertyValue {" PropertyName ")轻松完成,如下所示:
ProfileBase curProfile = ProfileBase.Create("MyName");
Run Code Online (Sandbox Code Playgroud)
或访问当前用户的个人资料:
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();
Run Code Online (Sandbox Code Playgroud)
在Visual Studio中创建新的Web站点项目时,将自动(自动)为您生成从Profile返回的对象.创建Web应用程序项目或MVC项目时,您必须自己动手.
这听起来可能比现在更困难.您需要执行以下操作:
| 归档时间: |
|
| 查看次数: |
58289 次 |
| 最近记录: |