本文实例讲述了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程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com