ASP.NET页面使用的Extern类

时间:2022-09-24 23:41:35

I'm a newbie in using C# applied with ASP.NET, so I ask you of all things patience.

我是使用C#应用ASP.NET的新手,所以我问你所有的耐心。

First the context: I developed an ASP page who takes to validate a username and a password (as shown in the first chunk of code. For effects of this question, it doesn't matter about the characters in the password box, it's irrelevant).

首先是上下文:我开发了一个ASP页面,用于验证用户名和密码(如第一块代码所示。对于这个问题的效果,它与密码框中的字符无关,它是无关的) 。

Index.aspx

的Index.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Login" runat="server">
    <div><table>
    <tr>
    <td>User</td>
    <td><asp:TextBox ID="User" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><asp:TextBox ID="Pass" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td>
    <td><asp:Button ID="LoginButton" runat="server" Text="Login" 
        onclick="LoginButton_Click" /></td>
    </tr></table>
    </div>
    </form>
    </body>
    </html>

Then after clicking the button "Login", the strings given in both textboxes are being compared with specific strings, and if both strings coincide, the login is successful (as shown in the second chunk of code).

然后在单击“登录”按钮后,将两个文本框中给出的字符串与特定字符串进行比较,如果两个字符串重合,则登录成功(如第二个代码块所示)。

Index.aspx.WebDesigner.cs

Index.aspx.WebDesigner.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LoginBoton_Click(object sender, EventArgs e)
        {
            String user = User.Text;
            String password = Pass.Text; 

            String uservalid = "Carlos";
            String passvalid = "236";

            if((user.Equals(uservalid)) && (password.Equals(passvalid)))
                Response.Redirect("Valid.aspx");
            else
                Response.Redirect("Invalid.aspx");

        }
    }
}

Let's suppose in some moment I need to create a new class specifically for validating the login (I know it can be done with Java), and I will use it for my page. Is it necessary considering in this case I am already using the Index.aspx.WebDesigner.cs? And if it is necessary, or I don't have no choice but to use this new class, how can I create it?

让我们假设在某些时候我需要创建一个专门用于验证登录的新类(我知道它可以用Java完成),我会将它用于我的页面。是否有必要考虑在这种情况下我已经在使用Index.aspx.WebDesigner.cs?如果有必要,或者我别无选择,只能使用这个新课程,我该如何创建它?

1 个解决方案

#1


2  

Creating classes in c# is very similar to creating classes in any modern, strongly typed, OO programming language. First you define the class, and then you instantiate it. There are many different ways to re-create the validation in your question, here's one.

在c#中创建类与在任何现代强类型OO编程语言中创建类非常相似。首先定义类,然后实例化它。在您的问题中有许多不同的方法可以重新创建验证,这是一个。

Here is the class definition

这是类定义

public class Validator
{
  private const string Username = "Carlos";
  private const string Password = "236";

  public bool Validate(string user, string pass) 
  {
    return (user == Username && pass == Password);
  }
}

To instantiate and use the class in your code (note the use of the ternary conditional operator instead of if/else, this keeps the code concise and readable)

要在代码中实例化和使用该类(请注意使用三元条件运算符而不是if / else,这样可以使代码简洁易读)

protected void LoginBoton_Click(object sender, EventArgs e)
{
  //instantiate the class defined above
  var validator = new Validator();

  //find the next page to redirect to
  var redirectTo = validator.Validate(User.Text, Pass.Text) ? "Valid.aspx" : "Invalid.aspx";

  //redirect the user
  Response.Redirect(redirectTo);
}

C# is a deep language with a gentle learning curve, you may benefit from finding a good tutorial or book on the subject. There are a number of introductory tutorials from Microsoft that may be helpful.

C#是一种深度语言,具有温和的学习曲线,您可以从找到关于该主题的优秀教程或书籍中受益。 Microsoft提供了许多可能有用的入门教程。

Another thing to note, is that the word extern is a keyword in c#, that indicates managed code (i.e. code that runs in the CLR) wants to load and execute unmanaged code, (i.e. code that runs natively).

另外需要注意的是,extern这个词是c#中的一个关键字,它表示托管代码(即在CLR中运行的代码)想要加载和执行非托管代码(即本机运行的代码)。

#1


2  

Creating classes in c# is very similar to creating classes in any modern, strongly typed, OO programming language. First you define the class, and then you instantiate it. There are many different ways to re-create the validation in your question, here's one.

在c#中创建类与在任何现代强类型OO编程语言中创建类非常相似。首先定义类,然后实例化它。在您的问题中有许多不同的方法可以重新创建验证,这是一个。

Here is the class definition

这是类定义

public class Validator
{
  private const string Username = "Carlos";
  private const string Password = "236";

  public bool Validate(string user, string pass) 
  {
    return (user == Username && pass == Password);
  }
}

To instantiate and use the class in your code (note the use of the ternary conditional operator instead of if/else, this keeps the code concise and readable)

要在代码中实例化和使用该类(请注意使用三元条件运算符而不是if / else,这样可以使代码简洁易读)

protected void LoginBoton_Click(object sender, EventArgs e)
{
  //instantiate the class defined above
  var validator = new Validator();

  //find the next page to redirect to
  var redirectTo = validator.Validate(User.Text, Pass.Text) ? "Valid.aspx" : "Invalid.aspx";

  //redirect the user
  Response.Redirect(redirectTo);
}

C# is a deep language with a gentle learning curve, you may benefit from finding a good tutorial or book on the subject. There are a number of introductory tutorials from Microsoft that may be helpful.

C#是一种深度语言,具有温和的学习曲线,您可以从找到关于该主题的优秀教程或书籍中受益。 Microsoft提供了许多可能有用的入门教程。

Another thing to note, is that the word extern is a keyword in c#, that indicates managed code (i.e. code that runs in the CLR) wants to load and execute unmanaged code, (i.e. code that runs natively).

另外需要注意的是,extern这个词是c#中的一个关键字,它表示托管代码(即在CLR中运行的代码)想要加载和执行非托管代码(即本机运行的代码)。