ASP.NET中Global和URLReWrite用法

(编辑:jimmy 日期: 2024/10/6 浏览:2)

本文实例讲述了ASP.NET中Global和URLReWrite用法。分享给大家供大家参考。具体如下:

Global.asax:

有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。

重点了解:application_Start; application_BeginRequest; application_Error;

① application_Start:自从服务器启动起来,网站第一次被访问的时候Application_Start执行
② Application_Error :程序中发生未处理异常
③ Session_End:只有进程内的Session才会调用,session_End进程外的Session不会
④ application_BeginRequest:当一个请求过来的时候,便会调用application_BeginRequest,访问静态页面时application_BeginRequest不会处理,IIS直接将静态页面文件给了浏览器。即使访问一个不存在的页面,Application_BeginRequest方法也会被调用。

URLReWrite:

丑链接:http://localhost/viewPerson.aspx"/ViewPerson.aspx" + id); 搞定,整成【帅链接】,就这么简单。

使用正则表达式:

protected void Application_BeginRequest(object sender, EventArgs e)
{
  Match match = Regex.Match(Context.Request.Path, @"^/ViewPerson\-(\d+)\.aspx$");
  if (match.Success)
  {
 string id = match.Groups[1].Value;//拿到(\d+)就是id 的值 
 Context.RewritePath("/ViewPerson.aspx" + id);
  }
}

希望本文所述对大家的asp.net程序设计有所帮助。

一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。