【301重定向】

1 static void Redirect301(string url)
2 {
3       HttpContext.Current.Response.StatusCode = 301;
4       HttpContext.Current.Response.RedirectLocation = url;
5       HttpContext.Current.Response.End();
6 }
【串行参数】
1  static string GetQueryString(NameValueCollection list)
2  {
3        StringBuilder sb = new StringBuilder();          
4        for (int i=0; i<list.Count; i++)
5        {               
6          sb.AppendFormat("{0}={1}&",list.GetKey(i),HttpUtility.UrlEncode(list[i]));
7        }
8        return sb.ToString().Trim('&');
9  }

【将带~/ 的相对地址转为绝对地址】

 1  static string ResolveUrl(string url)
 2 {
 3     string approot = HttpContext.Current.Request.ApplicationPath;
 4     if (url.Length == 0 || url[0] != '~')
 5         return url;
 6     else
 7     {
 8         if (url.Length == 1)
 9             return approot;  
10         if (url[1] == '/' || url[1] == '\\')
11         {
12             // url: ~/  或者  ~\
13             if (approot.Length > 1)
14                 return approot + "/" + url.Substring(2);
15             else
16                 return "/" + url.Substring(2);
17         }
18         else
19         {
20             // url: ~xxxxx
21             if (approot.Length > 1)
22                 return approot + "/" + url.Substring(1);
23             else
24                 return approot + url.Substring(1);
25         }
26     }
27 }

【UrlEncode || UrlDecode】

UrlEncode || UrlDecode
 1 static string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
 2 
 3 public static string UrlEncode(this string value)
 4 {
 5     StringBuilder result = new StringBuilder();
 6     foreach (char symbol in value)
 7     {
 8         if (unreservedChars.IndexOf(symbol) > -1)
 9         {
10             result.Append(symbol);
11         }
12         else
13         {
14             result.AppendFormat("%{0:X2}", (int)symbol);
15         }
16     }
17     return result.ToString();
18 }
19 
20 public static string UrlEncode(this string value, Encoding encode)
21 {
22     StringBuilder result = new StringBuilder();
23     byte[] data = encode.GetBytes(value);
24     int len = data.Length;
25 
26     for (int i = 0; i < len; i++)
27     {
28         int c = data[i];
29         if (c < 0x80 && unreservedChars.IndexOf((char)c) != -1)
30         {
31             result.Append((char)c);
32         }
33         else
34         {
35             result.Append('%' + String.Format("{0:X2}", (int)data[i]));
36         }
37     }
38 
39     return result.ToString();
40 }

【Timestamp】

Timestamp
1 public static string GetTimestamp()
2 {
3     var date1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
4     var timeStamp = (long)DateTime.UtcNow.Subtract(date1970).TotalSeconds;
5     return timeStamp.ToString();
6 }

【request】

1 public XDocument GetXml(string url)
2 {
3        XDocument doc = null;
4        var str = new WebClient().DownloadString(url);
5        doc = XDocument.Parse(str);
6        return doc;
7  }
 1 public XDocument  GetXml(string apiUrl)
 2 { 
 3   XDocument doc = null;
 4   var request = HttpWebRequest.Create(apiUrl) as HttpWebRequest;
 5   request.Method = "POST";
 6   try
 7   {
 8      var response = request.GetResponse() as HttpWebResponse;
 9      if (response.StatusCode == HttpStatusCode.OK)
10       {
11             using (var sr = new StreamReader(response.GetResponseStream()))
12            {
13                    doc = XDocument.Parse(sr.ReadToEnd());
14            }
15 
16        }
17   }
18   catch (WebException ex)
19   {
20         throw ex;
21   }
22 
23   return doc;
24 }

 【Url重写】

UrlRewriteModule
 1 public class UrlRewriteModule : IHttpModule
 2     {   
 3         public void Dispose()
 4         {
 5         }
 6 
 7         public void Init(HttpApplication context)
 8         {
 9             context.BeginRequest += new EventHandler(context_BeginRequest);
10         }
11 
12         void context_BeginRequest(object sender, EventArgs e)
13         {
14             var application = (HttpApplication)sender;
15             var context = application.Context;
16             var request = context.Request;
17             var url = request.Path;
18            var newUrl=GetRewriteUrl(url);
19             var parms=new NameValueCollection();           
20                 parms["id"] = 101; 
21                 parms["type"]=2;           
22             context.RewritePath(newUrl, String.Empty,   GetQueryString(parms));
23        }
24 }
Web.Config
1 <?xml version="1.0"?>
2 <configuration>
3    <system.web>
4      <httpModules>
5          <add name="RewriteModule" type="UrlRewrite.UrlRewriteModule, UrlRewrite"/>
6        </httpModules>
7   </system.web>
8 </configuration>

 


 

 

posted on 2012-12-02 17:23  极简  阅读(189)  评论(0编辑  收藏  举报