• Url 相关操作


    【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>

  • 相关阅读:
    欧拉回路 定理
    UESTC 1087 【二分查找】
    POJ 3159 【朴素的差分约束】
    ZOJ 1232 【灵活运用FLOYD】 【图DP】
    POJ 3013 【需要一点点思维...】【乘法分配率】
    POJ 2502 【思维是朴素的最短路 卡输入和建图】
    POJ 2240 【这题貌似可以直接FLOYD 屌丝用SPFA通过枚举找正权值环 顺便学了下map】
    POJ 1860【求解是否存在权值为正的环 屌丝做的第一道权值需要计算的题 想喊一声SPFA万岁】
    POJ 1797 【一种叫做最大生成树的很有趣的贪心】【也可以用dij的变形思想~】
    js 实现slider封装
  • 原文地址:https://www.cnblogs.com/AspDotNetMVC/p/2796600.html
Copyright © 2020-2023  润新知