取的网站的绝对路径uri

这遍文章主要是解决了,你的程序以网站方式或是以虚拟目录方式展现时,图片路径等不影响。文采不好不知怎么表示请见下例网址:

http://www.cnblogs.com/lgh/a.jpg;

http://www.cnblogs.com/a.jpg;

红色部份是通过代码来定义的uri,不管你是以网站方式发布程序还是虚拟目录,都不影响程序。


请你读文章时,先了解uri和HttpContext知识,如果了解请直接看代码.

URI详细说明请见:http://www.cnblogs.com/skylaugh/archive/2007/03/30/693750.html

HttpContext是干什么的 详细介绍网址

http://www.cnblogs.com/adsiz/archive/2008/01/21/1047090.html

http://msdn.microsoft.com/zh-cn/library/system.web.httpcontext(VS.80).aspx

示例代码:来自 blogYi.net开源程序

ContractedBlock.gif ExpandedBlockStart.gif Code
 1public class Class1
 2ExpandedBlockStart.gifContractedBlock.gif{
 3    //取的得到网站的绝对URI
 4        public static Uri AbsoluteWebRoot
 5ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 6            get
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 8                HttpContext context = HttpContext.Current;
 9                if (context == null)
10                    throw new System.Net.WebException("The current HttpContext is null");
11
12                if (context.Items["absoluteurl"== null)
13                    context.Items["absoluteurl"= new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority) + RelativeWebRoot);
14
15                return context.Items["absoluteurl"as Uri;                
16            }

17        }

18        private static string _RelativeWebRoot;
19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
20        /// Gets the relative root of the website.
21        /// </summary>
22        /// <value>A string that ends with a '/'.</value>

23        public static string RelativeWebRoot
24ExpandedSubBlockStart.gifContractedSubBlock.gif        {
25            get
26ExpandedSubBlockStart.gifContractedSubBlock.gif            {
27                if (_RelativeWebRoot == null)
28                    _RelativeWebRoot = VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings["BlogEngine.VirtualPath"]);
29
30                return _RelativeWebRoot;
31            }

32        }

33    
34
35}

36

 

在web.config中加入    <add key="BlogEngine.VirtualPath" value="~/"/>

如果有虚拟目录,在value中输入虚拟目录名称。

 

 


转载于:https://www.cnblogs.com/lgh/archive/2008/10/08/1306104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值