变化的相对路径处理
2010-05-12 17:21
260 查看
ASP.NET中 使用Server.Transfer转移到别的地址;或使用自定义控件时。会出现相对地址不一定的情况。这时使用图片,Css文件或其他与相对路径相关的资源,就会出错。
处理这种情况需要使用代码。需要在Page上定义这样一个属性
该属性能够自动监测客户端页面的地址,生成相对路径。如果你有自定义的Page类,那么写在MyPage上会更方便。
使用时把原来的地址变为<%= CurrentPath +"从根开始的相对路径" %>
例如原来有一个背景图片: style="background-image:url('Imags/loginBg.jpg');"
需要修改为:style="background-image:url('<%= CurrentPath +"Imags/loginBg.jpg"%>');"
处理这种情况需要使用代码。需要在Page上定义这样一个属性
public string CurrentPath { get { //return "../"; string fileBase = HttpContext.Current.Request.PhysicalApplicationPath; string askFile = HttpContext.Current.Request.PhysicalPath; if (!askFile.Contains(fileBase)) return string.Empty;//错误。可以修改为异常 string xdpath = askFile.Replace(fileBase,""); xdpath = xdpath.Replace('//', '/'); int lasPos = 0; string rT = string.Empty ; for (int i = 0; i < xdpath.Length; i++) { if (xdpath[i] == '/') { if (lasPos != i) rT += "../"; lasPos = i; } } return rT; } }
该属性能够自动监测客户端页面的地址,生成相对路径。如果你有自定义的Page类,那么写在MyPage上会更方便。
使用时把原来的地址变为<%= CurrentPath +"从根开始的相对路径" %>
例如原来有一个背景图片: style="background-image:url('Imags/loginBg.jpg');"
需要修改为:style="background-image:url('<%= CurrentPath +"Imags/loginBg.jpg"%>');"
相关文章推荐
- 变化的相对路径处理
- 变化的相对路径处理
- 批处理中的相对路径
- 根据文件名取文件【含相对路径和绝对路径处理】
- jar包中相对路径处理
- Handlebars Paths Handlebars 路径处理 相当于html等静态资源引用的相对路径
- web服务器对url中的相对路径的处理("./", "//","../")
- 批处理相对路径51CTO自动领豆(Python)
- web服务器对url中的相对路径的处理("./", "//","../")
- PHP相对路径和绝对路径常见处理方法
- 实现图像处理算法中采用策略“Strategy”模式+Qt中相对路径
- 解决less的data-uri函数无法处理相对路径的Bug
- 处理Flash加载的相对路径
- 解决less的data-uri函数无法处理相对路径的Bug
- gradle项目中资源文件的相对路径打包处理技巧
- jar包中相对路径处理
- TinyMCE添加图片 路径自动处理成相对路径
- Python 爬取163整个网站的图片 多线程 深度控制 相对路径处理 请求间隔控制 正则匹配
- 批处理(.bat)文件中使用相对路径
- Xcode下的相对路径和绝对路径