Asp.net mvc 4 使用ApiController的会话状态
我想在ApicController(MVC4)中拥有自己的AppContext 应该是Asp.net mvc 4 使用ApiController的会话状态,asp.net-mvc-4,controller,session-state,Asp.net Mvc 4,Controller,Session State,我想在ApicController(MVC4)中拥有自己的AppContext 应该是 public class TestController : BaseApiController { [HttpGet] public IEnumerable<TestVM> GetAll() { // the test service is injected with SimpleInjector return _testService.Ge
public class TestController : BaseApiController
{
[HttpGet]
public IEnumerable<TestVM> GetAll()
{
// the test service is injected with SimpleInjector
return _testService.GetAll(**base.AppContext**);
}
}
这是我的IAppContext(将来它将有更多属性)
SessionState类以获取AppContext
public class SessionState : BaseSessionVariables
{
public static IAppContext AppContext
{
get { return SessionState.Get<IAppContext>("AppContext"); }
set { SessionState.Set("AppContext", value); }
}
}
公共类SessionState:BaseSessionVariables
{
公共静态IAppContext AppContext
{
获取{return SessionState.get(“AppContext”);}
set{SessionState.set(“AppContext”,value);}
}
}
这里是BaseSessionVariables类
public static HttpSessionState GetSession()
{
return HttpContext.Current.Session;
}
protected static T Get<T>(string key) where T : class
{
var session = BaseSessionVariables.GetSession();
if (session == null)
{
throw new Exception("No session");
}
return (session[key] as T);
}
公共静态HttpSessionState GetSession()
{
返回HttpContext.Current.Session;
}
受保护的静态T Get(字符串键),其中T:class
{
var session=BaseSessionVariables.GetSession();
if(会话==null)
{
抛出新异常(“无会话”);
}
返回(会话[键]作为T);
}
谢谢你的帮助 看看下面的实现。这会让你朝着正确的方向前进 更新了IAppContext-添加了setter
public interface IAppContext
{
IIdentity User { get; set; }
/// <summary> Gets the user id. </summary>
/// <value>The user id.</value>
int IdUser { get; set; }
}
新类-实现IAppContext并包装HttpContext会话。为了进行测试,您可以创建一个TestAppContextImplementation,它不依赖于会话,而是依赖于一些其他内存存储机制
public class AppContextImplementation : IAppContext
{
public IIdentity User
{
get { return HttpContext.Current.Session["User"] as IIdentity; }
set { HttpContext.Current.Session["User"] = value; }
}
int IdUser
{
get { return Convert.ToInt32(Session["IdUser"]); }
set { Session["IdUser"] = value; }
}
}
对于
apicontroller
,为自己构建一个DelegatingHandler
,并将您所有的好东西推到request.Properties
。然后,无论您是在测试还是在运行live,都可以从请求中检索它们。这样做的好处是,您对控制器中的会话没有依赖性
消息处理程序
公共类ContextHandler:DelegatingHandler
{
受保护的覆盖任务SendAsync(HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)
{
//获取要添加到请求中的糖果
var goodies=/*调用goodieGoodieYumYum*/
//将我们的好东西添加到请求中
request.Properties.Add(Constants.RequestKey_Goodies,Goodies);
//传递给下一个处理程序
返回base.sendaync(请求、取消令牌);
}
}
控制器动作
var goodies=(List)Request.Properties[Constants.RequestKey_goodies];
感谢您的提示,但ApicController中没有可重写的OnAction正在执行
public static HttpSessionState GetSession()
{
return HttpContext.Current.Session;
}
protected static T Get<T>(string key) where T : class
{
var session = BaseSessionVariables.GetSession();
if (session == null)
{
throw new Exception("No session");
}
return (session[key] as T);
}
public interface IAppContext
{
IIdentity User { get; set; }
/// <summary> Gets the user id. </summary>
/// <value>The user id.</value>
int IdUser { get; set; }
}
public abstract class BaseApiController: ApiController
{
public IAppContext AppContext {get; set;}
protected override void OnActionExecuting(
ActionExecutingContext filterContext)
{
AppContext = new AppContextImplementation();
}
}
public class AppContextImplementation : IAppContext
{
public IIdentity User
{
get { return HttpContext.Current.Session["User"] as IIdentity; }
set { HttpContext.Current.Session["User"] = value; }
}
int IdUser
{
get { return Convert.ToInt32(Session["IdUser"]); }
set { Session["IdUser"] = value; }
}
}
public class ContextHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
// get the goodies to add onto the request
var goodies = /* call to goodieGoodieYumYum */
// add our goodies onto the request
request.Properties.Add(Constants.RequestKey_Goodies, goodies);
// pass along to the next handler
return base.SendAsync(request, cancellationToken);
}
}
var goodies = (List<Goodie>)Request.Properties[Constants.RequestKey_Goodies];