首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Asp.Net第一章入门之后台处理程序

Asp.Net第一章入门之后台处理程序

作者头像
张哥编程
发布2024-12-19 09:31:19
发布2024-12-19 09:31:19
1920
举报
文章被收录于专栏:云计算linux云计算linux

Asp.Net

C#-->OOP-->Winform--Asp.Net

Asp.Net第一章入门之后台处理程序_Handler
Asp.Net第一章入门之后台处理程序_Handler

1.新建空项目

2.建立html页面

login.html

代码语言:javascript
复制
<form action="handler/LoginHandler.ashx" method="post">
         账户:<input type="text" name="uname" /><br />
         密码:<input type="password" name="pwd" /><br />
         <button type="submit">提交</button>
     </form>

3.测试test.ashx

aspx​:Web窗体设计页面。Web窗体页由两部分组成:视觉元素(html、服务器控件和静态文本)和该页的编程逻辑(VS中的设计视图和代码视图可分别看到它们对应得文件)。VS将这两个组成部分分别存储在一个单独的文件中。视觉元素在.aspx 文件中创建 ​ashx:​.ashx文件是主要用来写web handler的。使用.ashx 可以让你专注于编程而不用管相关的web技术。我们熟知的.aspx是要做html控件树解析的,.aspx包含的所有html实际上是一个类,所有的html都是类里面的成员,这个过程在.ashx是不需要的。ashx必须包含IsReusable属性(这个属性代表是否可复用,通常为true),而如果要在ashx文件用使用Session必须实现IRequiresSessionState接口.

3.1 查看源码,理解HttpRequest、HttpResponse

代码语言:javascript
复制
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace demo01.handler
 {
     /// <summary>
     /// test 的摘要说明
     /// </summary>
     public class test : IHttpHandler
     {

         public void ProcessRequest(HttpContext context)
         {
             context.Response.ContentType = "text/plain";
             context.Response.Write("Hello World");
         }

         public bool IsReusable
         {
             get
             {
                 return false;
             }
         }
     }
 }

3.2 handler/LoginHandler.ashx

代码语言:javascript
复制
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace demo01.handler
 {
     /// <summary>
     /// LoginHandler 的摘要说明
     /// </summary>
     public class LoginHandler : IHttpHandler
     {

         public void ProcessRequest(HttpContext context)
         {
             context.Response.ContentType = "text/html";
             //context.Response.Write("Hello World");
             //我们下面的工作,就是需要通过请求对象,接受网页的数据
             string uname = context.Request.Params["uname"].ToString();
             //context.Response.Write(uname);
             string pwd = context.Request.Params["pwd"].ToString();

             //下一步需要判断,判断如果成功,则显示一句话,否则显示一句话 
             if ("admin".Equals(uname) && "123456".Equals(pwd))
             {
                 context.Response.Write("<font color='red'>成功登录!</font>");
             }
             else {
                 context.Response.Write("<font color='blue'>登录失败!</font>");
             }
         }

         public bool IsReusable
         {
             get
             {
                 return false;
             }
         }
     }
 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Asp.Net
  • 1.新建空项目
  • 2.建立html页面
  • 3.测试test.ashx
    • 3.1 查看源码,理解HttpRequest、HttpResponse
    • 3.2 handler/LoginHandler.ashx
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档