BotSharp/BotSharp.Channel.Weixin/Server.cs

102 lines
2.7 KiB
C#
Raw Normal View History

2018-11-01 15:29:31 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
#if NET45
using System.Web;
#else
using Microsoft.AspNetCore.Http;
#endif
namespace BotSharp.Channel.Weixin
{
public static class Server
{
private static string _appDomainAppPath;
public static string AppDomainAppPath
{
get
{
if (_appDomainAppPath == null)
{
#if NET45
_appDomainAppPath = HttpRuntime.AppDomainAppPath;
#else
_appDomainAppPath = AppContext.BaseDirectory; //dll所在目录;
#endif
}
return _appDomainAppPath;
}
set
{
_appDomainAppPath = value;
#if NETSTANDARD1_6 || NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1
if (!_appDomainAppPath.EndsWith("/"))
{
_appDomainAppPath += "/";
}
#endif
}
}
private static string _webRootPath;
/// <summary>
/// wwwroot文件夹目录专供ASP.NET Core MVC使用
/// </summary>
public static string WebRootPath
{
get
{
if (_webRootPath == null)
{
#if NET45
_webRootPath = AppDomainAppPath;
#else
_webRootPath = AppDomainAppPath + "wwwroot/";//asp.net core的wwwroot文件目录结构不一样
#endif
}
return _webRootPath;
}
set { _webRootPath = value; }
}
public static string GetMapPath(string virtualPath)
{
if (virtualPath == null)
{
return "";
}
else if (virtualPath.StartsWith("~/"))
{
return virtualPath.Replace("~/", AppDomainAppPath);
}
else
{
return Path.Combine(AppDomainAppPath, virtualPath);
}
}
public static HttpContext HttpContext
{
get
{
#if NET45
HttpContext context = HttpContext.Current;
if (context == null)
{
HttpRequest request = new HttpRequest("Default.aspx", "http://sdk.weixin.senparc.com/default.aspx", null);
StringWriter sw = new StringWriter();
HttpResponse response = new HttpResponse(sw);
context = new HttpContext(request, response);
}
#else
HttpContext context = new DefaultHttpContext();
#endif
return context;
}
}
}
}