在MVC的controller里默认是访问不到HttpContext的,如果直接调用HttpContext,你会发现这个对象总是空的。
下面是一个解决方法可以在Controller里取得当前请求的HttpContext,最好是放在一个基类里这样所有的controller都可以继承。
protected HttpContextBase HttpContext
{
get
{
HttpContextWrapper context =
new HttpContextWrapper(System.Web.HttpContext.Current);
return context;
}
}
Leave a Reply
You must be logged in to post a comment.