简体   繁体   English

在多个页面中重复使用相同的方法

[英]Re-using the same method across multiple pages

I have a method 我有办法

public void CheckLogin()
{
    if ((Session["UserName"] == "") || (Session["UserName"] == null))
    {
        Response.Redirect("Account/Login.aspx");
    }
}

I dont want rewrite it in every page. 我不想在每个页面上重写它。 How can I define this one time and call this function in other pages? 我该如何定义一次并在其他页面中调用此函数?

You generally put this in a utilities class. 您通常将其放在实用程序类中。 Mark the function as static. 将该功能标记为静态。 Put the utilities class in the App_Code folder (make this folder in the root of your site if you don't have one). 将实用程序类放在App_Code文件夹中(如果没有该文件夹,则将该文件夹放在网站的根目录中)。 Make sure your class has the appropriate using statements. 确保您的类具有适当的using语句。

using System;
using System.Web;

public class AuthenticationUtilities
    {
    public static void CheckLogin()
        {
        if (HttpContext.Current.Session["UserName"]==null || HttpContext.Current.Session["UserName"]=="")
            {
            HttpContext.Current.Response.Redirect("Account/Login.aspx");
            }
        }
    }

Then you call it from your pages like this... 然后像这样从页面调用它...

AuthenticationUtilities.CheckLogin();

By the way, you're reinventing the wheel. 顺便说一下,您正在重新发明轮子。 You should be using a technology such as Identity instead of rolling your own. 您应该使用诸如身份之类的技术,而不是自己动手做。 I'm only providing this answer in case you need to at another point create a function common to various pages. 我只是提供此答案,以防您需要在另一点上创建各个页面共有的功能。

Use inheritance to provide common functionality to your web pages. 使用继承为网页提供通用功能。

Create a page that performs the common task and then derive all your other pages from that page. 创建一个执行常规任务的页面,然后从该页面派生所有其他页面。

public class MyPage : MyBasePage

Using this approach, there's no need to explicitly call anything. 使用这种方法,无需显式调用任何东西。 It will all be handled in the base class, which you could then extend as needed. 它将全部在基类中处理,然后可以根据需要进行扩展。 All you need to remember is to change your class declaration so it inherits from your base class. 您需要记住的是更改类声明,使其从您的基类继承。

Another approach might be to stick this in a common master page. 另一种方法可能是将其放在公共母版页中。 But I don't like that approach as much. 但是我不太喜欢这种方法。

If you are doing such an authorization check, you probably want to handle the authorization event. 如果您正在执行此类授权检查,则可能要处理授权事件。 This is common enough that there is a built-in way to do it: 这很常见,以至于有一种内置方法:

From User-based Authorization ... 来自基于用户的授权 ...

Step 1: Defining URL Authorization Rules in Web.config 步骤1:在Web.config中定义URL授权规则

Step 2: Fixing the Workflow for Unauthorized, Authenticated User 步骤2:修正未经授权,经过身份验证的用户的工作流程

create a class file with a static class may be called "SessionCheck.cs" 创建带有静态类的类文件可能称为“ SessionCheck.cs”

and write this function in it as Static 并以静态形式写入此函数

public Static void CheckLogin()

    {

        if ((Session["UserName"] == "") || (Session["UserName"] == null))
        {

            Response.Redirect("Account/Login.aspx");

        }

    }

then you can access it from any page like 那么您可以从任何页面访问它

SessionCheck.CheckLogin();

Put it in a Session Helper class. 将其放在会话助手类中。

public class SessionHelper
{
    public static void CheckLogin()
    {
        if ((Session["UserName"] == "") || (Session["UserName"] == null))
        {
            Response.Redirect("Account/Login.aspx");
        }
    }
}

Then you call it like so. 然后您这样称呼它。

SessionHelper.CheckLogin();

create a class file who extend from Page, 创建一个从Page扩展的类文件,

public class Navegable: Page

so in the init function put your code 因此在init函数中将您的代码

void NavigablePage_PreInit(object sender, EventArgs e)
        {
if ((Session["UserName"] == "") || (Session["UserName"] == null))
        {
            Response.Redirect("Account/Login.aspx");
        }
}

so, the web form extend from navegable. 因此,网络表单从可导航性扩展。

MyPage : Navegable

thats is, all your webform who extend from navegable will run the validation 就是这样,您所有从navegable扩展的网络表单都将运行验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM