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的底层的工作机制,最近园子 ...
随机推荐
- Linux supervisord配置使用
supervisor官方网站 http://supervisord.org 1.安装supervisord Ubuntu: $sudo apt-get install python-setuptool ...
- C++移位运算符详解
移位运算符包括左移"<<"和右移">>" 左移运算符<<: 1.无符号 语法格式:需要移位的数字<<移位的次数n ...
- iOS开发优秀博客和软件推荐
iOSBlogAndTools iOS开发优秀博客和软件推荐 本博客和工具列表由广大iOS开发者收集和推荐,如果大家有好的博客或者工具想要分享请点击:我要提交. 收到大家的提交后会及时收录与更新.Gi ...
- Java Unicode编码 及 Mysql utf8 utf8mb3 utf8mb4 的区别与utf8mb4的过滤
UTF-8简介 UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,也是一种前缀码.它可以用来表示Unicode标准中的任何 ...
- Activity猫的一生-故事讲解Activity生命周期
大家好,关于Android中Activity的生命周期,网上大多数文章基本都是直接贴图.翻译API,比较笼统含糊不清. 我就用故事来说一说: 有个人叫User,TA养了几只猫,有只猫叫Activity ...
- Python使用心得之魔法参数**kw
通过设置字典类型参数直接传递给接收魔法参数(**kw)的方法,对应赋值.示例如下: jdbcConnectionDict = {, , 'database': 'test', 'charset': ' ...
- Linux 下安装 Memcached 和 PHP 开启 Memcached 扩展
[项目背景]:阿里云ECS服务器,Linux(centos7.2 64位),环境部署使用的是阿里云一键安装包(LAMP)等 [项目需求]:linux安装memcached 和php开启Memcache ...
- linux下的权限控制
终于还是要弄服务器了,这是多年前用fedora的时候整理的,也贴出来,顺便也再复习一下. 先来了解一下文件属性,在shell环境里输入:ls -l 可以查看当前目录文件.如:drwxr-xr-x. 1 ...
- 老李推荐: 第14章2节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-HierarchyViewer架构概述
老李推荐: 第14章2节<MonkeyRunner源码剖析> HierarchyViewer实现原理-HierarchyViewer架构概述 HierarchyViewer库的引入让M ...
- 1147: 零起点学算法54——Fibonacc
1147: 零起点学算法54--Fibonacc Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 20 ...