1、路径标识符。
- http标准定为:
- “/”代表网站根目录。
- “../”代表上级目录。
- “./”代表当前目录。
- Asp.net专用,所以要用在<asp: 控件中
<asp:HyperLink runat="server" NavigateUrl="~/a/b.aspx"></asp:HyperLink>
-
“~”转换为html能用的相对于应用根路径,经过转换为根路径,Html才认。
Response.Write("<a href='"+VirtualPathUtility.ToAbsolute("~/a/b.aspx"));
2、VirtualPathUtility类的一些主要方法:
-
string AppendTrailingSlash(string virtualPath)
-
string Combine(string basePath,string relativePath)
-
“~/a/b”+“c.aspx”=“~/a/c.aspx”
-
“~/a/b/”+“c.aspx”=“~/a/b/c.aspx”
-
string GetDirectory(string virtualPaht)
-
string MakeRelative(string fromPath,string toPath)
-
string ToAbsolute
3、Request是Page类的一个属性,所以在ashx中需要用context.Request
-
Request.AppRelativeCurrentExecutionFilePath
-
获取当前执行请求相对于应用根目录的虚拟目录。比如“~/Handle.ashx”
-
Request.PhysicalApplicationPath
-
Request.PhysicalPath
-
获取当前请求的物理路径。“D:a\a\b.aspx”
-
Request.RawUrl
-
Request.Url
-
Request.UrlReferrer
-
网页的来源,它去读取Http报文中的Referrer,通过这个做防盗链,全域中
-
用来防盗链,一般用于图片。因迅雷把所有网页正确的Request.UrlReferrer保存在一个库里,下载的时候伪造Referrer。
-
context.Response.ContentType = "image/JPEG";
string fullpath = HttpContext.Current.Server.MapPath("IMG_5433.JPG");
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
using (System.Drawing.Font font = new System.Drawing.Font("宋体", 30))
{
if (context.Request.UrlReferrer == null)
{
g.Clear(System.Drawing.Color.White);
g.DrawString("图片", font, System.Drawing.Brushes.Red, 0, 0);
}
else if(context.Request.UrlReferrer.Host!="localhost")
{
g.DrawString("仅供内部交流使用", font, System.Drawing.Brushes.Red, 0, 0);
}
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
-
Request.UserHostAddress
-
Request.UserLanguages
-
Request.Cookies
-
context.Request.Cookies[“mysessionID”]
-
Response.cookies[“”]
-
Request.MapPath(virtulPath)
-
将虚拟路径转换为磁盘上的物理路径,程序用的时候才稳妥。