Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何最好地处理C#ASP.NET web应用程序中的动态页面设置?_C#_Asp.net - Fatal编程技术网

如何最好地处理C#ASP.NET web应用程序中的动态页面设置?

如何最好地处理C#ASP.NET web应用程序中的动态页面设置?,c#,asp.net,C#,Asp.net,我有一个单独的页面,根据某些条件(例如,如果是访问该页面的用户或管理员),以特定方式配置了许多控件。我目前如何实现这一点是通过为所有页面通用的设置提供一个接口,然后扩展实现特定于用户类型的属性的类 例如: public interface Display_Type { string backgroundColor { get; } } public class AdminPage : Display_Type { string backgroundColor { get { r

我有一个单独的页面,根据某些条件(例如,如果是访问该页面的用户或管理员),以特定方式配置了许多控件。我目前如何实现这一点是通过为所有页面通用的设置提供一个接口,然后扩展实现特定于用户类型的属性的类

例如:

public interface Display_Type
{
    string backgroundColor { get; }
}

public class AdminPage : Display_Type
{
    string backgroundColor { get { return "orange"; } }
}

public class UserPage : Display_Type
{
    string backgroundColor { get { return "blue"; } }
}
我的页面的代码隐藏:

public partial class MyPage : System.Web.UI.Page
{
    Display_Type pageSettings;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((bool)Session["Is_Admin"])
        {
            pageSettings = new AdminPage();
        }
        else
        {
            pageSettings = new UserPage();
        }

        // ...

        string backgroundColor = pageSettings.backgroundColor;
        // ... Do stuff with background color
    }
}
这对我来说很好,但由于这些设置在整个应用程序中都是恒定的,所以它们作为静态类似乎更有意义。然而,我很难弄清楚如何做到这一点,因为我无法将静态类分配给变量

我的问题是:

  • 有没有更好的方法来完成我在这里要做的事情
  • 或者,如果可以的话,我如何完成我对静态类/成员所做的工作

  • 可能值得注意的是,用户/管理员示例不是我在web应用程序中如何使用此结构,事实上与用户本身无关,而是与请求参数等其他因素有关。

    将您的设置放在
    基本页上,并让其他页面从中派生出来

    您将只设置一次设置

    public abstract class MyBasePage : System.Web.UI.Page
    {
        protected Display_Type PageSettings { get; private set; };
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["Is_Admin"])
            {
                PageSettings = new AdminPage();
            }
            else
            {
                PageSettings = new UserPage();
            }
        }
    }
    
    public partial class MyPage : MyBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // ...
    
            string backgroundColor = PageSettings.backgroundColor;
            // ... Do stuff with background color
        }
    }
    

    这不是我要做的,但是如果你这样做的话,静态类绝对与此无关,在这种情况下也没有任何用处


    您可能会考虑每个DePyPyType类都有一个实例被存储在某个地方重新使用,而不是每次创建一个新的类。这可能最终成为一个静态变量。。。但这与静态类完全不同。

    您可以使用单例来定义
    AdminPage
    UserPage
    配置文件,并将静态方法
    GetDisplayType()
    添加到实现中

    public static class PageTypes {
        public static PageType Admin(/** stuff here */);
        public static PageType User(/** stuff here */);
    }
    
    public class PageType {
        readonly string _backgroundColor;
        public PageType (/** stuff here */) {
            _backgroundColor = backgroundColor;
        }
    
        public string BackgroundColor {
            get {
                return _backgroundColor;
            }
    }
    
    现在,您可以在方法中这样访问它们:

    if ((bool)Session["Is_Admin"])           
    {               
        PageSettings = PageTypes.Admin;    
    }           
    else           
    {               
        PageSettings = PageTypes.User;
    }
    

    我同意Jakub关于使用基类防止代码重复的观点。

    要回答您的第二个问题,静态解决方案的实现:

    public interface IDisplayType
    {
        string backgroundColor { get; }
    }
    
    public class AdminPage : IDisplayType
    {
        public string backgroundColor { get { return "orange"; } }
    }
    
    public class UserPage : IDisplayType
    {
        public string backgroundColor { get { return "blue"; } }
    }
    
    public static class PageProperties
    {
        private static AdminPage _adminPage = new AdminPage();
        private static UserPage _userPage = new UserPage();
    
        public static IDisplayType DisplayType { get
        {
            if ((bool)HttpContext.Current.Session["Is_Admin"])
             {
                return _adminPage;
             }
    
            return _userPage;
        }
     }
    
    我还将类型Display_type更改为IDisplayType,这可以更好地描述它是什么

    然后,您可以在页面中使用以下代码

    string backgroundColor = PageProperties.DisplayType.backgroundColor;
    
    1。有没有更好的方法来完成我在这里要做的事情?

    Microsoft.NET有一个内置的应用程序设置实现:设置文件。如果您的设置不能由每个用户配置,请使用它们

    应用程序设置可以在附属程序集中定义,并在应用程序的
    app.config
    web.config
    中重写

    我发现最好使用设置文件来定义这样的设置,这是一个内置的、可理解的、实现良好的解决方案,而且您可以开箱即用

    如何使用设置文件实现您的目标

    您可以按约定使用配置,背景色设置如下所示:

    • AdminPage_BackgroundColor=>#000000
    • 用户页面\u背景色
    • 等等
    在您的情况下,两个页面都有两个背景色设置,但如果需要为任何页面配置背景色,则可以在页面派生类实例中执行此操作:

    Properties.PagesStyle.Default[GetType().Name+'.'+“BackgroundColor”]

    您可以通过页面和设置文件获取背景色

    让我们在page类中实现这一点:

    public partial class MyPage : System.Web.UI.Page
    {
        Color backgroundColor = null;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["Is_Admin"])
            {
                backgroundColor = Properties.PagesStyle.Default.AdminPage_BackgroundColor;
            }
            else
            {
                backgroundColor = Properties.PagesStyle.Default.UserPage_BackgroundColor;
            }
    
            // ... Do stuff with background color
        }
    }
    
    注意:设置文件允许您定义强类型值。假设您键入“UserPage\u BackgroundColor”作为
    System.Drawing.Color
    ,设计器将使用颜色选择器编辑颜色,当您访问此设置时,您将得到一个
    System.Color

    在开始另一种方法之前,请检查以下有用的链接:

    • MSDN中的设置文件:
    • 我不久前还做了一些其他回答:

    推荐方法

    有一种可靠、稳定且有价值的网站样式设计方法:CSS

    不要创建自己的样式方法,只需使用CSS类即可

    例如,在某些CSS样式表中,您定义了管理页面和用户页面的背景色:

    想象一下,您有这个ASP.NET页面(我将只包括标准的XHTML标记):

    这样可以避免:

    • 创建冗余设置,因为您使用CSS作为样式机制(不要重新发明轮子!)
    • 编译样式值:这是客户端的事情,因为它是CSS

    静态类在这里毫无用处是什么意思?我认为,通过使它们成为静态的,从而使所有访问站点的用户在整个应用程序中都是全局的,我至少可以对性能产生边际影响。我这样想是不是错了?静态类与“应用程序的全局性”无关。它们与没有实例成员有关。另一方面,正如我所指出的,静态成员变量在这里可能很有用。因此,将类设置为静态与声明仅包含静态成员的非静态类的多个实例相比没有任何好处?您甚至无法创建静态类的单个实例,这使得静态类无法在本系统中使用。相反,应该在某个地方创建每个派生对象的单个实例,可能作为静态成员变量,或者使用缓存或应用程序。您可以使用Singleton模式来防止包含静态成员的类的多个实例化。它更有用,因为它可以被注入用于测试目的。我是否正确地假设这或多或少是我目前的做法,除了确定使用什么页面设置的逻辑被分配给这个基本页面之外?如果有什么区别的话,这些设置只在一个页面上使用,而不是在多个页面上使用
    body.UserPage
    {
        background-color: #000;
    }
    
    body.AdminPage
    {
        background-color: #FFF;
    }
    
    <html>
      <head></head>
      <body id="body" runat="server"></body>
    </html>
    
    public partial class MyPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["Is_Admin"])
            {
                body.Attributes["class"] = "AdminPage";
            }
            else
            {
                body.Attributes["class"] = "UserPage";
            }
        }
    }