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的底层的工作机制,最近园子 ...
随机推荐
- H5 音频
HTML 5 音频 HTML5 视频/DOM HTML5 拖放 HTML5 提供了播放音频的标准. Web 上的音频 直到现在,仍然不存在一项旨在网页上播放音频的标准. 今天,大多数音频是通过插件(比 ...
- 读书笔记 effective c++ Item 34 区分接口继承和实现继承
看上去最为简单的(public)继承的概念由两个单独部分组成:函数接口的继承和函数模板继承.这两种继承之间的区别同本书介绍部分讨论的函数声明和函数定义之间的区别完全对应. 1. 类函数的三种实现 作为 ...
- jquery.zclip.js 不起作用
最近有用到复制的这个功能,选用jQuery的插件,正常使用的时候,没有任何问题: $('#copy_mobile_watch_address').zclip({ path: '/dist/plugin ...
- Unity文档总结(2)-Understanding Automatic Memory Management
当一个对象.字符串.数组被创建的时候,从中间池申请分配需要存储的内存称为堆.当该部分不在使用时,一度占用的内存被释放收回,用于别的事物.在过去,它通常由开发人员分配和释放这些堆内存块,明确相应的功能调 ...
- Java中反射与常用方法
java通常是先有类再有对象,有对象我就可以调用方法或者属性. 反射其实是通过Class对象来调用类里面的方法.通过反射可以调用私有方法和私有属性.大部分框架都是运用反射原理. 如何获得Class ...
- Gson和Json
一下内容为复制别人的: Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. j ...
- 为什么你应该抛弃Express的视图渲染引擎
Nodejs Express框架的一个被人们广为使用的特性是它的渲染引擎.Express视图渲染引擎允许Controller提供一个视图名称和视图模型对象给Express,然后返回由HTTP响应流输出 ...
- raise RuntimeError("autoconf error") RuntimeError: autoconf error
pip 安装模块时遇到下错误,没有粘贴全,差不多都是这样.这个情况是 pip 安装模块 需要 gcc 及 python-devle 支持, ubuntu 是 python-dev ,使用Yum 安装即 ...
- 【Yii系列】最佳实践之后台业务框架
缘起 上面的几章都讲概念了,没有怎么讲到实践的东西,可能会有些枯燥,这很正常的,概念还是需要慢慢啃的,尤其是官网其他的部分,需要狠狠的啃. 什么,你啃不动了?看看官网旁边的那个在线用户吧. 你不啃的时 ...
- python学习随笔(二)
今天主要是用户输入输出,字符拼接. 在python中,多行注释是3个单引号或双引号 单行注释是#号开头,如下. ''' name = "xiaoming" print(name) ...