asp.net 基礎部分一
过程:
客户端像服务器发送一个请求,iis服务器接收到请求的数据,并且将数据交给c#程序进行处理,并且对数据库进行操作,并且将处理到的结果响应给浏览器客户端
过程2:
第一次浏览器请求,后端应该发一个表单给浏览器,然后再把表单的信息提交回服务器。其中要经过两次,一次请求,一次响应
2.本质:
本质上asp.net 是部署在iis服务器上的,所以一般性处理文件的核心是创建一个有权限处理对浏览器请求的一个类
3.网址解释:http:128.123.11.12:123 //前面的是服务器的id 后面是区分服务器中端口的实质
//注意:在打开http://localhost:6447/Handler1.ashx 需要设置到节点的文件上才能显示
4.另外,在测试登录信息是否正确的时候,可以在/后面加上?并且输入验证信息之后用& 来分隔信息
例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication5
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context) //请求报文
{
context.Response.ContentType = "text/plain"; //请求类型
string username=context.Request["username"]; //登录账号
if (username == "111")
{
context.Response.Write("Hello World"); //逻辑判断 并且响应
}
else
{
context.Response.Write("登录失败");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
//注意:
1 在""中 最好写‘’防止混乱
2 context.Response.ContentType = "text/html"; //请求类型 //关于请求类型
3 500是服务端报错 404是前端报错(地址输入的问题)没有这个资源 200是成功
例2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication5
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context) //请求报文
{
context.Response.ContentType = "text/html"; //请求类型
string username=context.Request["username"];
string password = context.Request["password"];
if (string.IsNullOrEmpty(username))
{
context.Response.Write("<form action='Handler1.ashx' method='post'<p>输入账号:<input type='text' name='username'/><p>输入密码:<input type='password'name='password'/><p><input type='submit' vlues='登录'</input></p></p></p></form>");
}
else
{
context.Response.Write("响应失败");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
例3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication5
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context) //请求报文
{
context.Response.ContentType = "text/html"; //请求类型
string username = context.Request["username"]; //请求对象获取属性
if (string.IsNullOrEmpty(username))
{
context.Response.Write("<form action='Handler1.ashx' method='post'<p>输入账号:<input type='text' name='username'/><p>输入密码:<input type='password'name='password'/><p><input type='submit' vlues='登录'</input></p></p></p></form>"); //响应
}
else {
if (username == "zhangsan")
{
context.Response.Write("有这个账号");
}
else
{
context.Response.Write("<form action='Handler1.ashx' method='post'<p>输入账号:<input type='text' name='username'/><p>输入密码:<input type='password'name='password'/><p><input type='submit' vlues='登录'</input></p></p></p><p style='color:red'>没有这个账号</p></form>");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
例4:利用io来读取html文件 实现样式分离 //反射是取程序集中的对象 而流是用来读取磁盘上的文件
//html部分
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action='Handler1.ashx' method='post'>
<p>输入账号:<input type='text' name='username'/></p>
<p>输入密码:<input type='password'name='password'/></p>
<p><input type="submit" value="提交" /></p>
</form>
</body>
</html>
//ashx 部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace WebApplication5
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context) //请求报文
{
context.Response.ContentType = "text/html"; //请求类型
string username = File.ReadAllText(@"D:\WebApplication5\WebApplication5\logo.html");
if (string.IsNullOrEmpty(username))
{
context.Response.Write(username);
}
注意:可以用对asp分离的样式进行设计
前端页面 logo.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action='Handler1.ashx' method='post'>
<p>输入账号:<input type='text' name='username'/></p>
<p>输入密码:<input type='password'name='password'/></p>
<p><input type="submit" value="提交" /></p>
{hahahah}
</form>
</body>
</html>
后端处理 asp.net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace WebApplication5
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context) //请求报文
{
context.Response.ContentType = "text/html"; //请求类型
string username = context.Request["username"];
string html =File.ReadAllText(@"C:\WebApplication5\WebApplication5\logo.html");
if (string.IsNullOrEmpty(username))
{
context.Response.Write(html);
}
else {
if (username == "lisi")
{
context.Response.Write("有这个账号");
}
else
{
context.Response.Write("<form action='Handler1.ashx' method='post'<p>输入账号:<input type='text' name='username'/><p>输入密码:<input type='password'name='password'/><p><input type='submit' vlues='登录'</input></p></p></p><p style='color:red'>没有获取到html地址</p></form>");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
4.在html页中使用特殊的字符然后在一般处理程序中替换html.Replace("字符","替换的内容")
例:
logo页面{eggrig}
一般处理程序页面
html = html.Replace("{eggrig}", "欢迎您使用本页面");
5.隐藏字段 通过在表单中设置一个隐藏字段 而判断隐藏字段是否为真,来确定非空验证
logo 页面
<input type="hidden" name="ispostback" value="true" />
后端页面
string ispostback = context.Request["ispostback"];
string html =File.ReadAllText(@"C:\WebApplication5\WebApplication5\logo.html");
if (string.IsNullOrEmpty(ispostback))
{
html = html.Replace("{eggrig}", "欢迎您使用本页面");
context.Response.Write(html);
}
引入数据库并且创建一个新的页面
步骤:
利用对数据拼接的技术进行
1.首先创建一个一般处理程序 写入查询的代码
2.再创建一个html前端的模板 并且将模板引入到一般处理程序之中
3.注意:context.Server.MapPath 可以将物理地址装换成相对地址
4.注意:后端不知道查出来是多少行的情况下可以用字符替换
//html 模板
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<table>
<tr>
<td>userID</td>
<td>username</td>
<td>password</td>
</tr>
<tr>
{conmetigjh}
</tr>
</table>
</body>
</html>
//一般处理程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace WebApplication5
{
/// <summary>
/// showstudents 的摘要说明
/// </summary>
public class showstudents : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
SqlConnection conn = new SqlConnection("server=.;database=SchoolIDB;uid=sa;pwd=123");
conn.Open();
string sql = "select * from users";
SqlCommand dx = new SqlCommand(sql, conn);
SqlDataAdapter cmd = new SqlDataAdapter(dx);
DataSet set = new DataSet();
cmd.Fill(set);
string html = File.ReadAllText(context.Server.MapPath("/showcunte.html"));
context.Response.Write(html);
DataRowCollection row = set.Tables[0].Rows;
for (int i = 0; i < row.Count; i++)
{
string y= row[i][0].ToString();
context.Response.Write(y);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
、
// string str = "";
DataRowCollection row = set.Tables[0].Rows;
for (int i = 0; i < row.Count; i++)
{
str = "<tr><td>" + row[i][0].ToString()+"</tr></td>"; //后端替换
}
string html = File.ReadAllText(context.Server.MapPath("/showcunte.html"));
html=html.Replace("{emmmadd}", str);
context.Response.Write(html);
}
//后端退换
asp.net 基礎部分一的更多相关文章
- GO語言基礎教程:序章
首先自我介紹一下我自己,我是一個coder,目前主要從事B/S程序開發工作,懂點PHP;ASP;JSP;JS;VB;C;DELPHI;JAVA,另外知道幾個數據庫,除此之外別無所長,那麼我為何會選擇學 ...
- Python 基礎 - for流程判斷
今天介紹另一個循環判斷式 for循環,首先,先寫一個很簡單的 for循環的代碼 #!/usr/bin/env python3 # -*- coding:utf-8 -*- for i in range ...
- Python 基礎 - if else流程判斷
hmm~前面講了那麼多,終於可以稍稍的正式進入另一個階段,沒錯,要開始寫判斷式了 這次先從最簡單的判斷式開始,if else 開始- Go 首先,之前有寫有一個簡單的互動式 用戶輸入 的代碼,忘記了嗎 ...
- JavaScript基礎知識
JavaScript基礎知識 1.標籤組使用 <script charset='utf-8' //設置字元集 defet //使腳本延遲到文檔解析完成,Browser已忽略 language=' ...
- BootStrap基礎知識
BootStrap基礎知識 1. .lead //突出 .text-left //文字居左 .text-right //文字居右 .text-center //文字居中 .text-justify / ...
- CSS1-3基礎知識
CSS1-3基礎知識 1.css排版 css在html內排版: <style type='text/css'> 標記名{} .類型名{} #ID名{} 標記名,.類型名,#ID名{} &l ...
- jQuery基礎知識
jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...
- 邁向IT專家成功之路的三十則鐵律 鐵律八:IT人學習之道-基礎功
修練過中國武術的人都知道,任何一種拳法的學習最重要的就是基礎功,而基礎功又可分為內在與外在的修練,內在的修練強調在平心.靜氣.不爭的調息.至於外在這首重在站樁.鬆沉.不疾不徐的應對能力.有了深厚基礎的 ...
- Python 基礎 - 淺copy補充說明
在 import copy 這個模塊裡 基於第一個列表來做淺copy,實際上第二個列表裡的元素,是第一個列表的 引用. 接下來介紹 淺copy有三種方式可以使用 #!/usr/bin/env pyth ...
随机推荐
- 响应者链条,如何获取最佳的点击view 以及内部实现
事件的产生与传递 事件是如何产生与传递的? 当发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中. UIApplication会从时间队列中取出最前面的时间,并将事件 ...
- RSA原理及生成步骤
摘自:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html(可到原网址查看秘钥生成原理) RSA算法原理(一) 因为它是 ...
- TFS工作区(Workspaces )命令
Workspaces 命令 tf workspaces [/owner:ownername] [/computer:computername] [/server:servername] [/forma ...
- Arcgis与CityEngine安装破解
Arcgis与CityEngine共存,实现同时破解 作为一个GIS背景的技术人员,以前安装了无数次的Arcgis DeskTop,到了新公司后,今天主管让我学习下CityEngine,学渣的我之前没 ...
- DatePicker 设置字体颜色
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #ffffff } span.s1 { } span.s2 { c ...
- Action向视图传值的6种方式
在使用ASP.NET MVC进行项目开发时,经常会碰到从Action向视图传值的问题,今天我就把我所知道的方式总结了一下,分成了以下六种: 1.使用ViewData进行传值 在Action中,有如下代 ...
- 《learning hard C#学习笔记》读书笔记(19)多线程
19.1 多线程编程知识 19.1.1 进程与线程的概念 进程: 可以理解为一块包含某些资源的内存区域,操作系统通过进程方式把它工作划分为不同的单元. 一个应用程序可以对应多个进程. 线程: 线程是进 ...
- 将jsp页面转化为图片或pdf(一)
在项目中遇见了将jsp页面转化为pdf的问题,试过itext,但是itext需要标准的html代码,我的页面中的一些属性是itext所不识别的,所以努力了一段时间后就放弃了,后来发现htmlutil抓 ...
- SPOJ - PLSQUARE Palin Squar(hash+回文串)
题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...
- UVA 12300 Smallest Regular Polygon(正多边形)
题意:给出两点,求经过这两点的正n边形的最小面积 题解:这两点一定是最长的弦,我们设正多边形中点c,找到c到每个点的距离(都相同) 我们知道那个等腰三角形的底与每个角度就使用余弦定理 #include ...