温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:PersonalBlog源码
当前文件:
CommunityServerPersonalBlog/AppCode/Common/WebContext.cs[11K,2009-6-12 11:37:08],打开代码结构图
CommunityServerPersonalBlog/AppCode/Common/WebContext.cs[11K,2009-6-12 11:37:08],打开代码结构图1using System; 2
using System.Collections.Generic; 3
using System.Collections.Specialized; 4
using System.IO; 5
using System.Threading; 6
using System.Web; 7
using System.Collections; 8
using NetFocus.Web.Core; 9
10
namespace NetFocus.Web.Applications.PersonalBlog 11
...{ 12
public sealed class WebContext 13
...{ 14
Private Members#region Private Members 15
16
private HybridDictionary _items = new HybridDictionary(); 17
private NameValueCollection _queryString = null; 18
private string _siteUrl = null; 19
private Uri _currentUri; 20
string rolesCacheKey = null; 21
bool _isUrlReWritten = false; 22
string _rawUrl; 23
HttpContext _httpContext = null; 24
DateTime requestStartTime = DateTime.Now; 25
26
#endregion 27
28
Initialize and cnstr.'s#region Initialize and cnstr.'s 29
30
private void Initialize(NameValueCollection qs, Uri uri, string rawUrl, string siteUrl) 31
...{ 32
_queryString = qs; 33
_siteUrl = siteUrl; 34
_currentUri = uri; 35
_rawUrl = rawUrl; 36
37
} 38
private WebContext(Uri uri, string siteUrl) 39
...{ 40
Initialize(new NameValueCollection(), uri, uri.ToString(), siteUrl); 41
} 42
private WebContext(HttpContext context, bool includeQS) 43
...{ 44
this._httpContext = context; 45
46
if (includeQS) 47
...{ 48
Initialize(new NameValueCollection(context.Request.QueryString), context.Request.Url, context.Request.RawUrl, GetSiteUrl()); 49
} 50
else 51
...{ 52
Initialize(null, context.Request.Url, context.Request.RawUrl, GetSiteUrl()); 53
} 54
} 55
56
#endregion 57
58
Create#region Create 59
60
public static WebContext Create(HttpContext context, UrlReWriterHandler rewriter) 61
...{ 62
WebContext webContext = new WebContext(context, false); 63
SaveContextToStore(webContext); 64
webContext.IsUrlReWritten = rewriter(context); 65
webContext._queryString = new NameValueCollection(context.Request.QueryString); 66
return webContext; 67
} 68
69
#endregion 70
71
Core Properties#region Core Properties 72
73
string userIdentityName = null; 74
public string UserIdentityName 75
...{ 76
get 77
...{ 78
return userIdentityName; 79
} 80
set 81
...{ 82
userIdentityName = value; 83
} 84
} 85
86
public IDictionary Items 87
...{ 88
get ...{ return _items; } 89
} 90
91
public object this[string key] 92
...{ 93
get 94
...{ 95
return this.Items[key]; 96
} 97
set 98
...{ 99
this.Items[key] = value; 100
} 101
} 102
103
public NameValueCollection QueryString 104
...{ 105
get ...{ return _queryString; } 106
} 107
108
public bool IsWebRequest 109
...{ 110
get ...{ return this.HttpContext != null; } 111
} 112
113
public bool IsAuthenticated 114
...{ 115
get ...{ return !_httpContext.User.Identity.IsAuthenticated; } 116
} 117
118
public HttpContext HttpContext 119
...{ 120
get 121
...{ 122
return _httpContext; 123
} 124
} 125
126
public DateTime RequestStartTime ...{ get ...{ return requestStartTime; } } 127
public string RolesCacheKey ...{ get ...{ return rolesCacheKey; } set ...{ rolesCacheKey = value; } } 128
public bool IsUrlReWritten ...{ get ...{ return _isUrlReWritten; } set ...{ _isUrlReWritten = value; } } 129
public string RawUrl ...{ get ...{ return _rawUrl; } set ...{ _rawUrl = value; } } 130
public Uri CurrentUri 131
...{ 132
get 133
...{ 134
if (_currentUri == null) 135
_currentUri = new Uri("http://localhost/cs"); 136
137
return _currentUri; 138
139
} 140
set ...{ _currentUri = value; } 141
} 142
private string _hostPath = null; 143
public string HostPath 144
...{ 145
get 146
...{ 147
if (_hostPath == null) 148
...{ 149
string portInfo = CurrentUri.Port == 80 ? string.Empty : ":" + CurrentUri.Port.ToString(); 150
_hostPath = string.Format("{0}://{1}{2}", CurrentUri.Scheme, CurrentUri.Host, portInfo); 151
} 152
return _hostPath; 153
} 154
} 155
156
#endregion 157
158
Helpers#region Helpers 159
160
public Guid GetGuidFromQueryString(string key) 161
...{ 162
Guid returnValue = Guid.Empty; 163
string queryStringValue; 164
165
queryStringValue = QueryString[key]; 166
167
if (queryStringValue == null) 168
...{ 169
return returnValue; 170
} 171
172
try 173
...{ 174
if (queryStringValue.IndexOf("#") > 0) 175
...{ 176
queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#")); 177
} 178
returnValue = new Guid(queryStringValue); 179
} 180
catch ...{ } 181
182
return returnValue; 183
184
} 185
public int GetIntFromQueryString(string key, int defaultValue) 186
...{ 187
string queryStringValue; 188
189
queryStringValue = this.QueryString[key]; 190
191
if (queryStringValue == null) 192
...{ 193
return defaultValue; 194
} 195
try 196
...{ 197
if (queryStringValue.IndexOf("#") > 0) 198
...{ 199
queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#")); 200
} 201
defaultValue = Convert.ToInt32(queryStringValue); 202
} 203
catch ...{ } 204
205
return defaultValue; 206
207
} 208
public string MapPath(string path) 209
...{ 210
if (_httpContext != null) 211
return _httpContext.Server.MapPath(path); 212
else 213
// Returns System\WOW for non web // return Directory.GetCurrentDirectory() + path.Replace("/", @"\").Replace("~", ""); 214
return PhysicalPath(path.Replace("/", Path.DirectorySeparatorChar.ToString()).Replace("~", "")); 215
} 216
public string PhysicalPath(string path) 217
...{ 218
return string.Concat(RootPath().TrimEnd(Path.DirectorySeparatorChar), Path.DirectorySeparatorChar.ToString(), path.TrimStart(Path.DirectorySeparatorChar)); 219
} 220
221
private string _rootPath = null; 222
private string RootPath() 223
...{ 224
if (_rootPath == null) 225
...{ 226
_rootPath = AppDomain.CurrentDomain.BaseDirectory; 227
string dirSep = Path.DirectorySeparatorChar.ToString(); 228
229
_rootPath = _rootPath.Replace("/", dirSep); 230
} 231
return _rootPath; 232
} 233
private string GetSiteUrl() 234
...{ 235
string hostName = _httpContext.Request.Url.Host.Replace("www.", string.Empty); 236
string applicationPath = _httpContext.Request.ApplicationPath; 237
238
if (applicationPath.EndsWith("/")) 239
applicationPath = applicationPath.Remove(applicationPath.Length - 1, 1); 240
241
return hostName + applicationPath; 242
243
} 244
245
#endregion 246
247
Common QueryString Properties#region Common QueryString Properties 248
249
private int entityId = 0; 250
private int parentId = 0; 251
private int categoryId = 0; 252
private string subject = null; 253
private string tags = null; 254
private int pageIndex = 0; 255
256
public int EntityId 257
...{ 258
get 259
...{ 260
if (entityId == 0) 261
...{ 262
entityId = GetIntFromQueryString("EntityId", 0); 263
} 264
return entityId; 265
} 266
set 267
...{ 268
entityId = value; 269
} 270
} 271
public int ParentId 272
...{ 273
get 274
...{ 275
if (parentId == 0) 276
...{ 277
parentId = GetIntFromQueryString("ParentId", 0); 278
} 279
return parentId; 280
} 281
set 282
...{ 283
parentId = value; 284
} 285
} 286
public int CategoryId 287
...{ 288
get 289
...{ 290
if (categoryId == 0) 291
...{ 292
categoryId = GetIntFromQueryString("CategoryId", 0); 293
} 294
return categoryId; 295
} 296
set 297
...{ 298
categoryId = value; 299
} 300
} 301
public string Subject 302
...{ 303
get 304
...{ 305
if (string.IsNullOrEmpty(subject)) 306
...{ 307
subject = QueryString["subject"]; 308
} 309
return subject; 310
} 311
set 312
...{ 313
subject = value; 314
} 315
} 316
public string Tags 317
...{ 318
get 319
...{ 320
if (string.IsNullOrEmpty(tags)) 321
...{ 322
tags = QueryString["tags"]; 323
} 324
return tags; 325
} 326
set 327
...{ 328
tags = value; 329
} 330
} 331
public int PageIndex 332
...{ 333
get 334
...{ 335
if (pageIndex == 0) 336
...{ 337
pageIndex = GetIntFromQueryString("PageIndex", GetIntFromQueryString("p", 0)); 338
if (pageIndex != 0) 339
...{ 340
pageIndex = pageIndex - 1; 341
} 342
else if (pageIndex < 0) 343
...{ 344
pageIndex = 0; 345
} 346
} 347
return pageIndex; 348
} 349
set 350
...{ 351
pageIndex = value; 352
} 353
} 354
355
#endregion 356
357
State#region State 358
359
private static readonly string dataKey = "CurrentHttpContextStore"; 360
361
public static WebContext Current 362
...{ 363
get 364
...{ 365
HttpContext httpContext = HttpContext.Current; 366
WebContext context = null; 367
if (httpContext != null) 368
...{ 369
context = httpContext.Items[dataKey] as WebContext; 370
} 371
else 372
...{ 373
context = Thread.GetData(GetSlot()) as WebContext; 374
} 375
376
if (context == null) 377
...{ 378
if (httpContext == null) 379
throw new Exception("No WebContext exists in the Current Application. AutoCreate fails since HttpContext.Current is not accessible."); 380
381
context = new WebContext(httpContext, true); 382
SaveContextToStore(context); 383
} 384
return context; 385
} 386
} 387
388
private static LocalDataStoreSlot GetSlot() 389
...{ 390
return Thread.GetNamedDataSlot(dataKey); 391
} 392
393
private static void SaveContextToStore(WebContext context) 394
...{ 395
if (context.IsWebRequest) 396
...{ 397
context.HttpContext.Items[dataKey] = context; 398
} 399
else 400
...{ 401
Thread.SetData(GetSlot(), context); 402
} 403
404
} 405
406
public static void Unload() 407
...{ 408
Thread.FreeNamedDataSlot(dataKey); 409
} 410
411
#endregion 412
413
} 414
}






}