关于global.asax中取物理路径和应用程序路径的问题,急。。

jiezi316 2007-09-04 09:09:00
我想在global.asax做一些操作。需要用到应用程序路径和当前的物理根路径。
物理根路径可以用AppDomain.CurrentDomain.BaseDirectory来获取。但应用程序路径就不知道怎么获取了。因为是在global.asax里面,其他的获取方式都不行了。(比如什么httpcontext.request啊什么的。因为根本就没有请求的对象。)

应用程序路径是指这个是部署在虚拟目录还是站点的那个路径。如果是站点返回的是“/”,如果是TEST虚拟目录则返回的是“/TEST”
...全文
286 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JGood 2007-09-09
  • 打赏
  • 举报
回复
用这个:System.Web.HttpContext.Current.Request.ApplicationPath
shixiaodan 2007-09-08
  • 打赏
  • 举报
回复
创建global.asax.vb或global.asax.cs
在Application_Start中就可以用HttpContext.Current.Request.ApplicationPath了
jiezi316 2007-09-04
  • 打赏
  • 举报
回复
static public string ApplicationRootPath = HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath;

就是这个方法。但在global.asax中不能用了。因为HttpContext.Current.Request为null.现在明白了没有。各位大大们?
jiezi316 2007-09-04
  • 打赏
  • 举报
回复
在global.asax中的什么HTTPCONTEXT.current.request这个是NULL。。另外用Server.MapPath("~/")取的是虚拟目录的物理路径。不是我说的那个。不是说清楚了嘛。

如果是站点返回的是“/”,如果是TEST虚拟目录则返回的是“/TEST”
在request对象中可以取的。但在global.asax中不能用
jiezi316 2007-09-04
  • 打赏
  • 举报
回复
..你们试过没有哟。在GLOBAL。ASAX中可以用吗?晕到。不能用啊。。其他地方可以用。
活靶子哥哥 2007-09-04
  • 打赏
  • 举报
回复
不知道你在说什么貌似是ApplicationPath ??? ,

给你一堆,自己找

#region Url/App Paths

/// <summary>
/// ヘセヤュハシオリヨキ like」コhttp://www.aspxboy.com
/// </summary>
public static string BaseUrl
{
get
{
return HttpContext.Current.Request.Url.GetLeftPart( UriPartial.Authority );
//return SiteConfiguration.GetConfig().SiteConfigedUrl;
}
}


/// <summary>
/// ヘセヤュハシオリヨキ like」コhttp://www.aspxboy.com:8080
/// </summary>
/// <returns></returns>
public static string GetHostUrl()
{
string securePort = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
string protocol = securePort == null || securePort == "0" ? "http" : "https";
string serverPort = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
string port = serverPort == "80" ? string.Empty : ":" + serverPort;
string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];

return string.Format( "{0}://{1}{2}" , protocol , serverName , port );
}

public static string HostPath( Uri uri )
{
string portInfo = uri.Port == 80 ? string.Empty : ":" + uri.Port.ToString();
return string.Format( "{0}://{1}{2}" , uri.Scheme , uri.Host , portInfo );
}

public static string FullPath( string local )
{
if ( IsNullOrEmpty( local ) )
{
return local;
}

if ( local.ToLower().StartsWith( "http://" ) || local.ToLower().StartsWith( "https://" ) )
{
return local;
}

if ( HttpContext.Current == null )
{
return local;
}

return FullPath( HostPath( HttpContext.Current.Request.Url ) , local );
}

public static string FullPath( string hostPath , string local )
{
return hostPath + local;
}

public static string GetSiteUrl()
{
string baseUrl = null;
HttpContext c = HttpContext.Current;
if ( c != null )
{
string port = c.Request.ServerVariables["SERVER_PORT"];
if ( port == null || port.Equals( "80" ) || port.Equals( "443" ) )
{
port = String.Empty;
}
else
{
port = ":" + port;
}

string protocol = c.Request.ServerVariables["SERVER_PORT_SECURE"];

if ( protocol == null || protocol.Equals( "0" ) )
{
protocol = "http://";
}
else
{
protocol = "https://";
}

baseUrl = protocol + c.Request.ServerVariables["SERVER_NAME"] + port + c.Request.ApplicationPath;
}

return baseUrl;
}

/// <summary>
/// モヲモテウフミ鯑篦キセカ like: http://www.aspxboy.com/dir
/// </summary>
public static string SiteUrl
{
get
{

return BaseUrl + ApplicationPath;
}
}

static public string ApplicationPath
{

get
{
string applicationPath = "/";

if ( HttpContext.Current != null )
{
applicationPath = HttpContext.Current.Request.ApplicationPath;
}

if ( applicationPath == "/" )
{
return string.Empty;
}
else
{
return applicationPath;
}
}

}

/// <summary>
/// Resolves a URL completely. The passed in URL must begin with ~, http://, or https://. For example,
/// the input ~/ShowFAQ.aspx would return http://www.somesite.com/dir1/ShowFAQ.aspx, assuming that the
/// application was located in the /dir1 directory. Essentially this method is a cheap knock-off of
/// the Control.ResolveUrl() method, but instead of returning just a relative path, it returns a full,
/// legal URL.
/// </summary>
public static string ResolveUrl( string relativeUrl )
{
if ( relativeUrl.StartsWith( "~" ) )
{
return Globals.SiteUrl + relativeUrl.Substring( 1 );
}
else if ( relativeUrl.StartsWith( "http://" ) || relativeUrl.StartsWith( "https://" ) )
{
return relativeUrl;
}
else
{
throw new ArgumentException( "URLs passed to Helpers.ResolveUrl() must begin with ~, http://, or https://" );
}
}

#endregion
purple_tide 2007-09-04
  • 打赏
  • 举报
回复
使用 Server.MapPath("~/") 取得的就是虚拟路径
活靶子哥哥 2007-09-04
  • 打赏
  • 举报
回复
昨天好像见过这个帖子

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧