加入收藏 | 设为首页 | 会员中心 | 我要投稿 南京站长网 (https://www.025zz.com.cn/)- 自然语言处理、建站、经验、云计算、图像分析!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

C#中实现伪静态页面两种方式说明

发布时间:2023-06-13 15:00:31 所属栏目:语言 来源:转载
导读:   第一种是在页面global.asax中,相关代码如下:



  代码如下:



  void Application_BeginRequest(object sender, EventArgs e)



  {



  HttpContext conte
  第一种是在页面global.asax中,相关代码如下:
 
  代码如下:
 
  void Application_BeginRequest(object sender, EventArgs e)
 
  {
 
  HttpContext context = ((HttpApplication)sender).Context;
 
  string oldurl = context.Request.Path.ToLower();
 
  if ( ( oldurl.IndexOf("-") > 0 && oldurl.IndexOf(".") == -1) || (oldurl.IndexOf("-") > 0 && oldurl.IndexOf("aspx") > 0) )
 
  {
 
  string[] url = oldurl.Substring(oldurl.LastIndexOf("/") + 1).Replace(".aspx", "").Split('-');
 
  string path = oldurl.Substring(0, oldurl.LastIndexOf("/") + 1);
 
  //file
 
  string file = url[0];
 
  file = file.Replace("about", "detail");
 
  file = file.Replace("news", "list");
 
  file = file.Replace("down", "detail");
 
  file = file.Replace("case", "album");
 
  file = file.Replace("contact", "detail");
 
  //query
 
  string query = "";
 
  for ( int i=1;i<url.Length;i++ )
 
  {
 
  if (url[i] != "")
 
  {
 
  switch (i)
 
  {
 
  case 1:
 
  query += "id=" + url[i];
 
  break;
 
  case 2:
 
  query += "&page=" + url[i];
 
  break;
 
  case 3:
 
  query += "&key=" + url[i];
 
  break;
 
  case 4:
 
  query += "&v1=" + url[i];
 
  break;
 
  case 5:
 
  query += "&v2=" + url[i];
 
  break;
 
  case 6:
 
  query += "&v3=" + url[i];
 
  break;
 
  case 7:
 
  query += "&v4=" + url[i];
 
  break;
 
  case 8:
 
  query += "&v5=" + url[i];
 
  break;
 
  case 9:
 
  query += "&v6=" + url[i];
 
  break;
 
  case 10:
 
  query += "&v7=" + url[i];
 
  break;
 
  }
 
  }
 
  }
 
  //newurl
 
  string newurl = path + file + ".aspx?" + query;
 
  if( context.Request.ServerVariables["QUERY_STRING"] != null && context.Request.ServerVariables["QUERY_STRING"] != "" )
 
  newurl += "&" + context.Request.ServerVariables["QUERY_STRING"];
 
  //Response.Write(newurl);
 
  context.RewritePath(newurl);
 
  }
 
  第二种方法是在HttpModule.cs中,代码如下:
 
  代码如下:
 
  public class HttpModule : IHttpModule
 
  {
 
  private const RegexOptions regexOptions = RegexOptions.IgnoreCase | RegexOptions.Compiled;
 
  private static readonly Regex regexFileName = new Regex(@".*?/([^./]*)/.aspx(.*)", regexOptions);
 
  private static readonly Regex regexRewritePath = new Regex(@"^.*?/(/w*)(-?(/w+)-([/w,/|,%]+))+/.aspx", regexOptions);
 
  public void Dispose()
 
  {
 
  }
 
  public void Init(HttpApplication httpApplication)
 
  {
 
  httpApplication.BeginRequest += ReUrl_BeginRequest;
 
  }
 
  private static void ReUrl_BeginRequest(object sender, EventArgs e)
 
  {
 
  Globals.Catch(
 
  () =>
 
  {
 
  var context = ((HttpApplication)sender).Context;
 
  var request = context.Request;
 
  var url = request.Url;
 
  if (!VerifyUrl(url))
 
  {
 
  string input = url.PathAndQuery.ToLower();
 
  //Loger.Debug("PathAndQuery-->" + input);
 
  //Loger.Debug("AbsolutePath-->" + url.AbsolutePath);
 
  //Loger.Debug("AbsoluteUri-->" + url.AbsoluteUri);
 
  //Loger.Debug("DnsSafeHost-->" + url.DnsSafeHost);
 
  //Loger.Debug("LocalPath-->" + url.LocalPath);
 
  //Loger.Debug("AppDomain.CurrentDomain.BaseDirectory-->" + AppDomain.CurrentDomain.BaseDirectory);
 

(编辑:南京站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章