• 注册
当前位置:1313e > 默认分类 >正文

asp.net获取网站路径


引用自:
asp.net获取网站路径
http://hi.baidu.com/zhangfengbang/blog/item/5f99d74b0ce661f883025cbc.html
HttpRequest 成员
http://msdn.microsoft.com/zh-cn/library/system.web.httprequest_members(v=VS.80).aspx
ASP.NET获取网站根目录的url的函数,很简单
http://www.xueit.com/html/2009-03/21_932_00.html
 

 


网站在服务器磁盘上的物理路径: HttpRuntime.AppDomainAppPath
虚拟程序路径: HttpRuntime.AppDomainAppVirtualPath


任何于Request/HttpContext.Current等相关的方法, 都只能在有请求上下文或者页面时使用. 即在无请求上下文时,HttpContext.Current为null. 而上面提到的方法一直可用.

对于全局Cache对象的访问亦然.


 


示例:输出asp.net 网站路径。 

private void responseHtml()
    {
         System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
  
//输出:当前时间: 2007-08-30 11:03:49
         sb.Append(string.Format("当前时间: {0}", Server.HtmlEncode(DateTime.Now.ToString())));
         sb.Append(
"");

  
//当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
         sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.CurrentExecutionFilePath)));
         sb.Append(
"");

  
//获取当前应用程序的根目录路径: /aDirectory
         sb.Append(string.Format("获取当前应用程序的根目录路径: {0}", Server.HtmlEncode(Request.ApplicationPath)));
         sb.Append(
"");

  
//当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
         sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.FilePath)));
         sb.Append(
"");

  
//当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx
         sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.Path)));
         sb.Append(
"");

  
//获取当前正在执行的应用程序的根目录的物理文件系统路径: E:\Visual Studio 2005\
         sb.Append(string.Format("获取当前正在执行的应用程序的根目录的物理文件系统路径: {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));
         sb.Append(
"");

  
//获取与请求的 URL 相对应的物理文件系统路径: E:\Visual Studio 2005\\aDirectory\
         sb.Append(string.Format("获取与请求的 URL 相对应的物理文件系统路径: {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));
         sb.Append(
"");
         Response.Write(sb.ToString());
     }

 



在ASP.NET编程中经常需要用Request获取url的有关信息.

测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath:                     /testweb
Request.CurrentExecutionFilePath:       /testweb/default.aspx
Request.FilePath:                                /testweb/default.aspx
Request.Path:                                     /testweb/default.aspx
Request.PathInfo:
Request.PhysicalApplicationPath:          E:\WWW\testweb\
Request.PhysicalPath:                         E:\WWW\testweb\default.aspx
Request.RawUrl:                                 /testweb/default.aspx
Request.Url.AbsolutePath:                    /testweb/default.aspx
Request.Url.AbsoluteUri:                      http://www.test.com/testweb/default.aspx
Request.Url.Host:                                www.test.com
Request.Url.LocalPath:                        /testweb/default.aspx 

当url中带参数时可以使用:
HttpContext.Current.Request.Url.PathAndQuery.ToString()//

本页地址:   Request.URL; 

上页地址:  
Request.UrlReferrer  
Request.ServerViables["http_referer"]  
Request.RawUrl  
Request.RawUrl.QueryAndPath  
System.IO.Path.GetFileName(Request.FilePath.ToString()) 




获取网站根目录的url源代码--2010.05.09
http://www.xueit.com/html/2009-03/21_932_00.html

public static string GetRootURI()
    {
        
string AppPath = "";
        HttpContext HttpCurrent 
= HttpContext.Current;
        HttpRequest Req;
        
if (HttpCurrent != null)
        {
            Req 
= HttpCurrent.Request;
            
string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
            
if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                
//直接安装在   Web   站点   
                AppPath = UrlAuthority;
            
else
                
//安装在虚拟子目录下   
                AppPath = UrlAuthority + Req.ApplicationPath;
        }
        
return AppPath;
    }

可修改为静态属性 

private
 static string rootURI;

    
/// 
    
/// 获得网站根目录的url的函数
    
/// 

    
/// 应用程序根目录 eg:  http://www.xxx.net:2156/im
    public static string RootURI
    {
        
get
        {
            
if (string.IsNullOrEmpty(rootURI))
            {
                HttpContext HttpCurrent 
= HttpContext.Current;
                HttpRequest Req;
                
if (HttpCurrent != null)
                {
                    Req 
= HttpCurrent.Request;

                    
string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                    
if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                        
//直接安装在   Web   站点   
                        rootURI = UrlAuthority;
                    
else
                        
//安装在虚拟子目录下   
                        rootURI = UrlAuthority + Req.ApplicationPath;
                }
            }

            
return rootURI;
        }
    }

转载于:https://www.cnblogs.com/kiant71/archive/2010/05/09/1751992.html

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录