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;
            }
        }

    }
}

转载于:https://www.cnblogs.com/lenya/archive/2012/09/05/3706518.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值