📄 webcontext.cs
字号:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Web;
using System.Collections;
using NetFocus.Web.Core;
namespace NetFocus.Web.Applications.Forum
{
public sealed class WebContext
{
#region Private Members
private HybridDictionary _items = new HybridDictionary();
private NameValueCollection _queryString = null;
private string _siteUrl = null;
private Uri _currentUri;
private string rolesCacheKey = null;
private bool _isUrlReWritten = false;
private string _rawUrl;
private HttpContext _httpContext = null;
private DateTime requestStartTime = DateTime.Now;
private ForumUser user = null;
#endregion
#region Initialize and cnstr.'s
private void Initialize(NameValueCollection qs, Uri uri, string rawUrl, string siteUrl)
{
_queryString = qs;
_siteUrl = siteUrl;
_currentUri = uri;
_rawUrl = rawUrl;
}
private WebContext(Uri uri, string siteUrl)
{
Initialize(new NameValueCollection(), uri, uri.ToString(), siteUrl);
}
private WebContext(HttpContext context, bool includeQS)
{
this._httpContext = context;
if (includeQS)
{
Initialize(new NameValueCollection(context.Request.QueryString), context.Request.Url, context.Request.RawUrl, GetSiteUrl());
}
else
{
Initialize(null, context.Request.Url, context.Request.RawUrl, GetSiteUrl());
}
}
#endregion
#region Create
public static WebContext Create(HttpContext context, UrlReWriterHandler rewriter)
{
WebContext webContext = new WebContext(context, false);
SaveContextToStore(webContext);
webContext.IsUrlReWritten = rewriter(context);
webContext._queryString = new NameValueCollection(context.Request.QueryString);
return webContext;
}
#endregion
#region Core Properties
public IDictionary Items
{
get { return _items; }
}
public object this[string key]
{
get
{
return this.Items[key];
}
set
{
this.Items[key] = value;
}
}
public NameValueCollection QueryString
{
get { return _queryString; }
}
public bool IsWebRequest
{
get { return this.HttpContext != null; }
}
public HttpContext HttpContext
{
get
{
return _httpContext;
}
}
public DateTime RequestStartTime { get { return requestStartTime; } }
public string RolesCacheKey { get { return rolesCacheKey; } set { rolesCacheKey = value; } }
public bool IsUrlReWritten { get { return _isUrlReWritten; } set { _isUrlReWritten = value; } }
public string RawUrl { get { return _rawUrl; } set { _rawUrl = value; } }
public Uri CurrentUri
{
get
{
if (_currentUri == null)
_currentUri = new Uri("http://localhost/cs");
return _currentUri;
}
set { _currentUri = value; }
}
private string _hostPath = null;
public string HostPath
{
get
{
if (_hostPath == null)
{
string portInfo = CurrentUri.Port == 80 ? string.Empty : ":" + CurrentUri.Port.ToString();
_hostPath = string.Format("{0}://{1}{2}", CurrentUri.Scheme, CurrentUri.Host, portInfo);
}
return _hostPath;
}
}
#endregion
#region Helpers
public Guid GetGuidFromQueryString(string key)
{
Guid returnValue = Guid.Empty;
string queryStringValue;
queryStringValue = QueryString[key];
if (queryStringValue == null)
{
return returnValue;
}
try
{
if (queryStringValue.IndexOf("#") > 0)
{
queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));
}
returnValue = new Guid(queryStringValue);
}
catch { }
return returnValue;
}
public int GetIntFromQueryString(string key, int defaultValue)
{
string queryStringValue;
queryStringValue = this.QueryString[key];
if (queryStringValue == null)
{
return defaultValue;
}
try
{
if (queryStringValue.IndexOf("#") > 0)
{
queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));
}
defaultValue = Convert.ToInt32(queryStringValue);
}
catch { }
return defaultValue;
}
public string MapPath(string path)
{
if (_httpContext != null)
return _httpContext.Server.MapPath(path);
else
// Returns System\WOW for non web // return Directory.GetCurrentDirectory() + path.Replace("/", @"\").Replace("~", "");
return PhysicalPath(path.Replace("/", Path.DirectorySeparatorChar.ToString()).Replace("~", ""));
}
public string PhysicalPath(string path)
{
return string.Concat(RootPath().TrimEnd(Path.DirectorySeparatorChar), Path.DirectorySeparatorChar.ToString(), path.TrimStart(Path.DirectorySeparatorChar));
}
private string _rootPath = null;
private string RootPath()
{
if (_rootPath == null)
{
_rootPath = AppDomain.CurrentDomain.BaseDirectory;
string dirSep = Path.DirectorySeparatorChar.ToString();
_rootPath = _rootPath.Replace("/", dirSep);
}
return _rootPath;
}
private string GetSiteUrl()
{
string hostName = _httpContext.Request.Url.Host.Replace("www.", string.Empty);
string applicationPath = _httpContext.Request.ApplicationPath;
if (applicationPath.EndsWith("/"))
applicationPath = applicationPath.Remove(applicationPath.Length - 1, 1);
return hostName + applicationPath;
}
#endregion
#region Common QueryString Properties
#region Private Members
private int roleId = 0;
private int entityId = 0;
private int parentId = 0;
private int sectionId = 0;
private int groupId = 0;
private string subject = null;
private string tags = null;
private int pageIndex = 0;
private int userId = 0;
private string userName = null;
private string picFileName = null;
private int picWidth = -2;
private int picHeight = -2;
private string defaultPicFileName = null;
private string backColor = null;
private bool userRealSize = true;
private string fileUrl = null;
#endregion
public int EntityId
{
get
{
if (entityId == 0)
{
entityId = GetIntFromQueryString("EntityId", 0);
}
return entityId;
}
set
{
entityId = value;
}
}
public int ParentId
{
get
{
if (parentId == 0)
{
parentId = GetIntFromQueryString("ParentId", 0);
}
return parentId;
}
set
{
parentId = value;
}
}
public int SectionId
{
get
{
if (sectionId == 0)
{
sectionId = GetIntFromQueryString("SectionId", 0);
}
return sectionId;
}
set
{
sectionId = value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -