制作内幕 游戏 网站 图标 | 理论 图标在线制作 | 看图软件 访谈活动 | 教程 技术 万年历
当前位置:首页>CG 教 程>网站开发>

相关文章

推荐文章

     

热门文章

得到网站路径信息的一个小函数

作者: 出处: 发布时间:2006-05-26 字体: [ ]
最近有个通用系统,因为系统需要发布一些XML文件到网站的固定位置中,但是系统在网站的位置却是非固定的,所以就需要判断网站的网址。虽然可以在WEB.CONFIG文件中设置,但改来改去,还是有些麻烦。我们可以通过定义一个函数对网站的网址加以判断。

  比如现在目录是:website/article/admin/send.aspx,那么可以得到如下信息:
  目录深度:PathDepth(2)
  网站根目录相对路径:HomeRelativeSite(../../)
  网址:HomeSite(website/)
  HomePhysicalPath:网站物理路径
  PagePhysicalPath:当前页物理路径

  先定义结构

 

/// <summary>
/// 当前页面执行时的其它页面等信息
/// 路径、网址均以"/"关闭
/// </summary>
public struct WebPageInfo
{
public int PathDepth; // 当前目录相对根目录的深度
public string HomeRelativeSite; // 根目录相对于本页的相对路径
public string HomeSite; // 网站网址
public string HomePhysicalPath; // 网站的物理路径
public string PagePhysicalPath; // 页面的物理路径
}

  函数:

 

   /// <summary>
/// 得到当前页面时的路径信息结构
/// </summary>
public static WebPageInfo GetCurrentPathInfo()
{
HttpContext context = HttpContext.Current;
WebPageInfo pageInfo = new WebPageInfo();

pageInfo.HomePhysicalPath =
context.Request.ServerVariables["APPL_PHYSICAL_PATH"];
pageInfo.PagePhysicalPath =
context.Request.ServerVariables["PATH_TRANSLATED"];

string splitStr = "\\/";
char[] ch = splitStr.ToCharArray();
string[] path1 = pageInfo.HomePhysicalPath.Split(ch);
string[] path2 = pageInfo.PagePhysicalPath.Split(ch);

// 路径深度
pageInfo.PathDepth = path2.Length - path1.Length;
// 网站根目录相对于本面的相对路径
pageInfo.HomeRelativeSite = "";
if (pageInfo.PathDepth > 0)
{
for (int i = 1; i <= pageInfo.PathDepth; i++)
{
pageInfo.HomeRelativeSite += "../";
}
}

// 得到网站网址
string tmpPath =
pageInfo.PagePhysicalPath.Substring(pageInfo.HomePhysicalPath.Length);
string tmpHttp =
"http://" + context.Request.ServerVariables["HTTP_HOST"]
+ context.Request.ServerVariables["PATH_INFO"];
pageInfo.HomeSite = tmpHttp.Substring(0, tmpHttp.Length - tmpPath.Length);

return pageInfo;
}

 


↑返回顶部   打印本页   关闭窗口↓  
收藏本文到:天极网摘 新浪VIVI 和讯网摘 博彩中心 365Key网摘 poco网摘 狐摘
亿友响享Yeeyoo igooi-it网摘 5seek网摘 I2Key 我摘网摘 天下图摘 YouNote 百特门
用户名: 新注册) 密码: 匿名评论 [所有评论]
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
设计服务 | 广告投放 | 友情链接 | 关于本站 | 联系我们 | 版权声明 | 设为首页 | 加入收藏