在ASP.Net基页获取页面的具体信息具体、页面、信息、ASP

2023-09-06 17:19:49 作者:伤心七寸 〃

我有以下的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);
}
 
asp页面读取html页面参数问题

在日志:

 公众覆盖字符串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"; }
}