mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Web;
|
|
|
|
namespace Orchard.Mvc {
|
|
public interface IHttpContextAccessor {
|
|
HttpContextBase Current();
|
|
void Set(HttpContextBase httpContext);
|
|
}
|
|
|
|
public class HttpContextAccessor : IHttpContextAccessor {
|
|
private HttpContextBase _httpContext;
|
|
|
|
public HttpContextBase Current() {
|
|
var httpContext = GetStaticProperty();
|
|
return httpContext != null ? new HttpContextWrapper(httpContext) : _httpContext;
|
|
}
|
|
|
|
public void Set(HttpContextBase httpContext) {
|
|
_httpContext = httpContext;
|
|
}
|
|
|
|
private HttpContext GetStaticProperty() {
|
|
var httpContext = HttpContext.Current;
|
|
if (httpContext == null) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
if (httpContext.Request == null) {
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception) {
|
|
return null;
|
|
}
|
|
return httpContext;
|
|
}
|
|
}
|
|
}
|