C#获取域名[/虚拟目录] 及物理目录

using System;
using System.Web;

namespace RTXEX.Common
{
    /// <summary>
    /// 全局类,获取当前域名、物理路径
    /// </summary>
    public class WebApplication
    {
        static string _appUrl = null;
        static string _appSiteName = null;
        static string _appMapPath = null;

        /// <summary>
        /// 获得url路径, http://localhost/aa/index.aspx 的app部分如:http://localhost/aa
        /// </summary>
        public static string AppUrl
        {
            get
            {
                //return "http://" + System.Web.HttpContext.Current.Request.Url.Host + AppSiteName;
                if (_appUrl == null)
                    _appUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + AppSiteName;

                return _appUrl;
            }
        }

        /// <summary>
        /// 获得 http://localhost/aa/index.aspx 的 虚拟应用名部分如:aa
        /// </summary>
        public static string AppSiteName
        {
            get
            {
                if (_appSiteName == null)
                    _appSiteName = HttpContext.Current.Request.ApplicationPath.Equals("/") ? string.Empty : HttpContext.Current.Request.ApplicationPath;

                return _appSiteName;
            }
        }

        /// <summary>
        /// 获得 http://localhost/aa/index.aspx 的物理路径 如:D:\work\vsProject\MvcApplication1
        /// </summary>
        public static string AppMapPath
        {
            get
            {
                if (_appMapPath == null)
                {
                    _appMapPath = HttpContext.Current.Server.MapPath("~");
                    _appMapPath = _appMapPath.EndsWith("\\") ? _appMapPath.Remove(_appMapPath.Length - 1) : _appMapPath;
                }

                return _appMapPath;
            }
        }

    }
}

posted @ 2012-09-05 17:28  lenya  阅读(1214)  评论(0编辑  收藏  举报