ASP.Net零碎
ASP.Net零碎
ServerPush
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery-2.1.1.js"></script>
<script type="text/javascript">
$(function () {
$("#send").click(function () {
var me = $("#me").val();
var tousername = $("#tousername").val();
var msgs = $("#msgs").val();
$.ajax({
type: "post", url: "ServerPush.ashx",
data: { action: "send", me: me, tousername: tousername, msgs: msgs },
success: function (data) {
if (data.Status =="ok" )
{
$("#ulMs").append($("<li>我对" + tousername + "说:" + msgs + "</li>"));
$("#msgs").val("");
}
else {
alert("发送出错,返回报文无法识别");
}
},
error: function () {
alert("发送出错");
}
})
})
$("#lode").click(function () {
var me = $("#me").val();
$.ajax({
type: "post", url: "ServerPush.ashx", data: { action: "serve", me: me },
success: function (data) {
$("#ulMs").append($("<li>" + data.Fromsername + "我对说:" + data.Msga + "</li>"));
},
error: function () {
alert("发送出错");
}
})
})
})
</script> </head>
<body>
发送者:<input type="text" id="me" />
<input type="button" id="lode" value="登陆" /><br />
接受者<input type="text" id="tousername" />
说:<input type="text" id="msgs" />
<input type="button" id="send" value="发送" />
<ul id="ulMs"></ul>
</body>
//Id, Tousername, Fromusername, Msgs
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string action = context.Request["action"];//判断是接受还是发送
if (action == "send")
{
string me = context.Request["me"];
string tousername = context.Request["tousername"];
string msg = context.Request["msgs"];
SQLHelper.SqlEffectRow("insert into Msg(Fromusername,Tousername,Msgs) values(@FromUserName,@ToUserName,@Msg)", new SqlParameter("@FromUserName", me), new SqlParameter("@ToUserName", tousername), new SqlParameter("@Msg", msg));//新增消息
context.Response.Write(new JavaScriptSerializer().Serialize(new{Status="ok"}) );
}
else if(action=="serve")
{
string me = context.Request["me"];
while (true)
{
DataTable Dt = SQLHelper.SQLDataTable("select top 1 * from Msg where Tousername=@Tousername", new SqlParameter("@Tousername", me));
if (Dt.Rows.Count <= )
{
Thread.Sleep();//休息500毫秒,减轻服务器压力
continue;
}
else
{
int id = (int)Dt.Rows[]["Id"];
string fromsername = (string)Dt.Rows[]["Fromusername"];
string msgs = (string)Dt.Rows[]["Msgs"];
SQLHelper.SqlEffectRow("delete from Msg where Id=@Id", new SqlParameter("@Id", id));//保存后删除该条消息,避免重复
var data = new { Fromsername = fromsername, Msga = msgs };
string json = new JavaScriptSerializer().Serialize(data);//json
context.Response.Write(json);
break;
}
}
}
Global
namespace Web1
{
public class Global : System.Web.HttpApplication
{
//自从服务器启动起来,网站第一次被访问的时候Application_Start执行
protected void Application_Start(object sender, EventArgs e)
{
File.AppendAllText("d:\\1.txt", DateTime.Now+"Application_Start\r\n");
} //Session启动时
protected void Session_Start(object sender, EventArgs e)
{
File.AppendAllText("d:\\1.txt", DateTime.Now + "Session_Start\r\n");
} //当一个请求过来的时候
//html等静态文件是iis直接把文件给到浏览器,不经过asp.net引擎的处理。
//所以不会调用Application_BeginRequest方法
protected void Application_BeginRequest(object sender, EventArgs e)
{
//即使用户访问一个不存在的页面,那么Application_BeginRequest也会被调用 File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_BeginRequest:"+
Context.Request.RawUrl + "\r\n");
//Context.RewritePath("WebExcel.html");//请求重写在服务器内部发生
//File.AppendAllText("d:\\1.txt", DateTime.Now + "Context.Request.Path:" +
//Context.Request.Path + "\r\n"); int? count =(int?)Application.Get("Count");
if (count == null)
{
count = ;
}
count++;
Application.Lock();
Application.Set("Count", count);
Application.UnLock(); //Url重写:UrlRewrite。ViewPerson-1.aspx
Match match = Regex.Match(Context.Request.Path, @"^/ViewPerson\-(\d+)\.aspx$");
if (match.Success)
{
string id = match.Groups[].Value;
Context.RewritePath("/ViewPerson.aspx?id="+id);
}
} protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ } //程序中发生未处理异常
protected void Application_Error(object sender, EventArgs e)
{
File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_Error:"+
Context.Error + "\r\n");
} //(*)Session过期(只有进程内Session,也就是InProc过期的时候才会调用Session_End)
protected void Session_End(object sender, EventArgs e)
{
File.AppendAllText("d:\\1.txt", DateTime.Now + "Session_End\r\n");
} protected void Application_End(object sender, EventArgs e)
{
File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_End\r\n");
}
}
}
UrlRewrite
//当一个请求过来的时候
//html等静态文件是iis直接把文件给到浏览器,不经过asp.net引擎的处理。
//所以不会调用Application_BeginRequest方法
protected void Application_BeginRequest(object sender, EventArgs e)
{
//即使用户访问一个不存在的页面,那么Application_BeginRequest也会被调用 File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_BeginRequest:"+
Context.Request.RawUrl + "\r\n");
//Context.RewritePath("WebExcel.html");//请求重写在服务器内部发生
//File.AppendAllText("d:\\1.txt", DateTime.Now + "Context.Request.Path:" +
//Context.Request.Path + "\r\n"); int? count =(int?)Application.Get("Count");
if (count == null)
{
count = ;
}
count++;
Application.Lock();
Application.Set("Count", count);
Application.UnLock(); //Url重写:UrlRewrite。ViewPerson-1.aspx
Match match = Regex.Match(Context.Request.Path, @"^/ViewPerson\-(\d+)\.aspx$");
if (match.Success)
{
string id = match.Groups[].Value;
Context.RewritePath("/ViewPerson.aspx?id="+id);
}
}
Application
ASP.Net缓存
//Cache是全局共享的
DataTable dt = (DataTable)HttpRuntime.Cache["persons"];
if (dt == null)//如果Cache中没有,再去数据库中查询
//这样可以降低数据库服务器的压力
{
dt = SqlHelper.ExecuteQuery("select * from T_Persons"); //存储缓存,30秒后过期
HttpRuntime.Cache.Insert("persons", dt, null,
DateTime.Now.AddSeconds(), TimeSpan.Zero);
} Repeater1.DataSource = dt;
Repeater1.DataBind();
母版页(*)和shtml
<!--#include file="head.html"-->
正文
<!--#include file="foot.html"-->
IIS配置
IIS配置文档:
1、安装IIS。控制面板→程序→打开关闭Windows功能,Web管理服务和万维网服务都勾上。
2、部署网站:ASP.Net项目的发布:项目中点右键“发布”,选择“文件系统”,发布到一个文件夹下。
3、在IIS中新建网站,设定域名,这样多个域名可以放到一个IIS服务器上。需要绑定域名。
4、模拟域名,如果启用了UAC,则用管理员权限运行记事本,打开
C:\Windows\System32\drivers\etc下的hosts文件
做一下域名协议的欺骗。伪造一些域名出来。
5、如果报错报错“无法识别的属性“targetFramework”,则:
1)、把网站的应用程序池的.net framework版本改成“4.0”
2)、C:\Windows\Microsoft.NET\Framework\v4.0.30319下用管理员权限运行( aspnet_regiis.exe -i )
6、默认文档问题,让用户访问www.web2.com的时候其实是访问www.web2.com/index.apsx:如果用户没有指定要访问哪个文件,则从上向下,匹配到谁,谁就是默认文档。
7、MSSQL的Windows身份登录在IIS运行的问题。IIS是以Windows服务( Windows服务的特点是:系统不登录的时候已经在运行)的形式运行,由于Windows服务默认不是用当前用户名运行的,那么IIS也就不是用当前用户名运行的,那么IIS的中运行的程序也不是以当前用户名运行的,因此asp.net程序运行所采用的用户名不是SQLServer的管理员用户,因此无法用“集成身份验证”登陆SQLServer,只能用“用户名密码方式登陆”
ASP.Net零碎的更多相关文章
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- ASP.NET Core中如影随形的”依赖注入”[上]: 从两个不同的ServiceProvider说起
我们一致在说 ASP.NET Core广泛地使用到了依赖注入,通过前面两个系列的介绍,相信读者朋友已经体会到了这一点.由于前面两章已经涵盖了依赖注入在管道构建过程中以及管道在处理请求过程的应用,但是内 ...
- [转]ASP.NET Core 中的那些认证中间件及一些重要知识点
本文转自:http://www.qingruanit.net/c_all/article_6645.html 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系 ...
- asp.net c# 网上搜集面试题目大全(附答案)
1.String str=new String("a")和String str = "a"有什么区别? String str = "a"; ...
- [转]asp.net c# 网上搜集面试题目(附答案)
本文转自:http://www.cnblogs.com/hndy/articles/2234188.html 1.String str=new String("a")和String ...
- 难得的中文ASP.NET 5/MVC 6入门教程
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于ASP.NET 5还未正式发布,即使是官方文档都还不完善,更不要说系统的中文文档了 ...
- [知识库分享系列] 二、.NET(ASP.NET)
最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
文章转自:http://blog.sina.com.cn/s/blog_568e66230100hy6a.html FCKEditor于2009年被更名为新一代的CKEditor和CKFinder,见 ...
- (翻译)从底层了解ASP.NET体系结构 [转]
转自:http://www.cnblogs.com/rijing2004/archive/2007/09/14/howaspnetwork.html 前言 关于ASP.NET的底层的工作机制,最近园子 ...
随机推荐
- 队列工厂之RedisMQ
本次和大家分享的是RedisMQ队列的用法,前两篇文章队列工厂之(MSMQ)和队列工厂之RabbitMQ分别简单介绍对应队列环境的搭建和常用方法的使用,加上本篇分享的RedisMQ那么就完成了咋们队列 ...
- jQuery中$.extend(true,object1, object2);深拷贝对象
语法:jQuery.extend( [deep ], target, object1 [, objectN ] ) 深浅拷贝对应的参数就是[deep],是可选的,为true或false.默认情况是fa ...
- Docker remote API简单配置使用
1.启动docker remote API的方式如下: docker -d -H uninx:///var/run/docker.sock -H tcp://0.0.0.0:5678 2.但是为了伴随 ...
- [cookie篇]从cookie-parser中间件说起
当我们在写web的时候,难免会要使用到cookie,由于node.js有了express这个web框架,我们就可以方便地去建站.在使用express时,经常会使用到cookie-parser这个插件. ...
- csvkit---python一个牛逼到不行的csv处理库
先吐槽一下:不管是百度还是谷歌,查来查去除了官方文档之外就没有任何可以借鉴的例子,虽然官方文档写的挺好的.但是我一直以为是在python语言的方式运行的,结果是以命令行的方式运行的,搞得我还以为这个库 ...
- Sublime Text 3下载-汉化-插件配置
Sublime Text 3下载 不用说是上官方下载地址:http://www.sublimetext.com/3 Sublime Text 3 免费使用方法 Sublime Text 2的时候还有一 ...
- loadrunner提高篇-插入检查点与关联函数
插入检查点 靠LR自动生成的脚本是不够的,很难达到业务要求,因此需要对录制完的脚本进行完善,使其能达到业务模拟的要求 ,这样尽可能地使虚拟用户模拟时更接近用户的实际使用. 在进行压力测试时,经常会 ...
- win7下nsis打包exe安装程序教程
下载软件包: NSIS中文版 :https://pan.baidu.com/s/1mitSQU0 装好之后会出现两个软件:Nullsoft Install System 和 VNISEdit 编译环境 ...
- 以正确的姿势实现一棵JavaScript菜单树
菜单树是常见的前端特效, 一般长下面这样 还有各种形态的变种, 有长这样的 也有长这样的 尽管这些菜单的相貌都不尽相同, 在功能实现的本质上却都是相同的.实现程序的大致流程如下 读取服务器端的菜单数据 ...
- Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added
很久之前测试通过的代码,现在手机升级了Android7.0后一运行就崩溃,报出这样的错误,具体错误如下: Process: com.example.sho.android_anti_theft, PI ...