在ASP.NET应用程序中使用配置文件 [英] Use a Profile in an ASP.NET Web Application

查看:159
本文介绍了在ASP.NET应用程序中使用配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  如何分配配置文件值?

我在读一本书ASP.NET,说如果你选择的Web应用程序,当你启动一个项目,你不能使用个人资料。它只网站下运行。

I'm reading an ASP.NET book that says you can't use Profile if you select Web Application when you start a project. It only run under Web Site.

是否有Web应用程序的任何替代方案?或者,你需要建立自己的档案系统。

Are there any alternatives for Web Application? Or do you need to build your own Profile system.

推荐答案

您,如果您使用的是Web应用程序,而不是一个网站,可以使用配置文件提供,但开箱从一个aspx页面code背后你将不能够做到 Profile.MyProperty

You can use the profile provider if you are using a web application instead of a web site, but out of the box from an aspx page code behind you will not be able to do Profile.MyProperty.

这是不是一个大问题与努力一点点,你就可以做同样的事情。这个例子中提供网站项目转换为Web应用程序项目是如何使用配置文件提供一个Web应用程序一个很好的起跳点。

This isn't a big deal as with a little bit of effort you will be able to do something similar. The example providing in Converting a Web Site Project to a Web Application Project is a good jumping off point on how to use the profile provider with a web application.

要总结上述条文的转换配置文件对象code段创建一个的ProfileCommon

To summarize the Converting Profile Object Code section of the aforementioned article create a ProfileCommon class

public class ProfileCommon
{
    public Teachers Teachers
    {
        get
        {
            return (Teachers)
                HttpContext.Current.Profile.GetPropertyValue("Teachers");
        }
        set
        {
            HttpContext.Current.Profile.SetPropertyValue("Teachers",value);
        }
    }
}

然后在你的aspx code后面你现在可以做的。

Then on your aspx code behind you can now do

ProfileCommon Profile = new ProfileCommon();
protected void Button1_Click(object sender, EventArgs e)
{
    Teachers teachers = new Teachers();
    teachers.Add(new Teacher("scott"));
    teachers.Add(new Teacher("bob"));
    teachers.Add(new Teacher("paul"));

    Profile.Teachers = teachers;    
}

要实例化一个替代的的ProfileCommon 为每个页面字段将使它和它的方法静态的,只需要调用从code屁股类的属性 Profile.Teachers

An alternative to instantiating the ProfileCommon as a field for each page would be to make it and its methods static and just call the class properties from the code behinds Profile.Teachers

public static class Profile
{
    public static Teachers Teachers
    {
        //......
    }
}

这是不是一个巨大的优势,但是让你的code更类似于一个ASP.NET网站项目。

This isn't a huge advantage, but makes your code more similar to that of a ASP.NET Web Site project.

社评不相关答案

有两个项目类型之间的主要区别,但我的项目,我preFER Web应用程序。我有偏见,因为开创了TFS构建经理的生成配置文件是Web应用程序项目更容易比它是为网站项目。而且它似乎像微软强调网站的项目,然后从他们赞成Web应用程序项目的支持了。

Editorial Commentary not pertinent to the answer
There are key differences between the two project types, but for my projects I prefer web applications. I have a bias because creating a build profile for the TFS Build Manager is much easier for Web Application projects than it is for Web Site projects. Further it seems like Microsoft emphasized Web Site projects and then backed away from them in favor of Web Application projects.

这篇关于在ASP.NET应用程序中使用配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆