在ASP.Net基页获取页面的具体信息 [英] Getting page specific info in ASP.Net Base Page

查看:90
本文介绍了在ASP.Net基页获取页面的具体信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Admin屏幕逻辑。我需要在日志页面也类似的逻辑。因此,我打算将这个逻辑为基础的页面。在基页,我怎么认识当前页面? (我如何分辨管理员页面和日志页面之间?)。

根据页面从配置检索到的值是不同的。

有哪些不同的方式来实现这一目标?什么是最好的出路,这些方法?

  //管理网
        名单<字符串> authorizedRoles =新的名单,其中,串>((ConfigurationManager.AppSettings [AdminScreenRoles]),斯普利特('')。)
        如果(!authorizedRoles.Contains(userRole))
        {
            的Response.Redirect(UnauthorizedPage.aspx);
        }

    //日志屏幕
        名单<字符串> authorizedRoles =新的名单,其中,串>((ConfigurationManager.AppSettings [LogsScreenRoles]),斯普利特('')。)
        如果(!authorizedRoles.Contains(userRole))
        {
            的Response.Redirect(UnauthorizedPage.aspx);
        }
 

解决方案

不要把code的基础,认识到继承它的类。添加一个抽象的财产,孩子将不得不重写。 在基地:

 公共抽象的字符串AppSettingsRolesName {获得; }

名单<字符串> authorizedRoles =新的名单,其中,串>((ConfigurationManager.AppSettings [AppSettingsRolesName])斯普利特('')。)
如果(!authorizedRoles.Contains(userRole))
{
    的Response.Redirect(UnauthorizedPage.aspx);
}
 

在日志:

 公众覆盖字符串AppSettingsRolesName
{
   {返回LogsScreenRoles; }
}
 

在管理员:

 公众覆盖字符串AppSettingsRolesName
{
   {返回AdminScreenRoles; }
}
 

I have following logic in Admin screen. I need similar logic in Logs screen also. Hence I am planning to move this logic into base page. In base page, how do I recognize the current page? (How do I distinguish between Admin screen and Logs screen?).

Based on the page the value retrieved from the config is different.

What are the different ways to achieve this? What is the best way out of these approaches?

        //Admin Screen
        List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings["AdminScreenRoles"]).Split(','))
        if (!authorizedRoles.Contains(userRole))
        {
            Response.Redirect("UnauthorizedPage.aspx");
        }

    //Logs Screen   
        List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings["LogsScreenRoles"]).Split(','))
        if (!authorizedRoles.Contains(userRole))
        {
            Response.Redirect("UnauthorizedPage.aspx");
        }

解决方案

Don't put code in base that recognize the class that inherit it. Add an abstract property that the child will have to override. In base:

public abstract string AppSettingsRolesName { get; }

List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings[AppSettingsRolesName]).Split(','))
if (!authorizedRoles.Contains(userRole))
{
    Response.Redirect("UnauthorizedPage.aspx");
}

In Logs:

public override string AppSettingsRolesName 
{
   get { return "LogsScreenRoles"; }
}

In Admin:

public override string AppSettingsRolesName 
{
   get { return "AdminScreenRoles"; }
}

这篇关于在ASP.Net基页获取页面的具体信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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