网页API行为过滤器 - Controller.TempData相同呢? [英] Web API Action Filter - Controller.TempData equivalent?

查看:166
本文介绍了网页API行为过滤器 - Controller.TempData相同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我System.Web.Mvc操作筛选pviously使用TempData的存储我的UnitOfWork服务的一个实例,像这样我$ P $:

 公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.Controller.TempData [UnitOfWorkRequestKey] =的UnitOfWork;
    UnitOfWork.Begin();
}

然后提交我retreived从这样的临时数据的事务。

 公共覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
{
    VAR的UnitOfWork =(IUnitOfWork)filterContext.Controller.TempData [UnitOfWorkRequestKey]
    尝试
    {
        如果(filterContext.Exception == NULL)
        {
            unitOfWork.Complete();
        }
    }
    最后
    {
        unitOfWork.Dispose();
        filterContext.Controller.TempData [UnitOfWorkRequestKey] = NULL;
    }
}

所以我的问题是:结果
System.Web.Http 网​​页API操作过滤器(使用 HttpActionContext ) - 有存储相当于我的位置服务的实例,因此,行动执行的时候,我可以取回相同的实例?


解决方案

  

在System.Web.Http网页API操作过滤器(使用HttpActionContext)
   - ?有没有来存储我的服务实例同等的位置,所以行动执行的时候,我可以取回相同的实例


没有,没有。 API的全部意义在于,它应该是无状态的。这就是规则编号1。如果你需要使用Session或TempData的你可能做的事情,从设计的角度来看非常错误的API中

此外,你不应该在你的MVC应用程序这个任务使用的TempData。当您需要多个请求之间仍然存在信息TempData的使用。你的情况是一样的请求。所以,你应该已经使用的HttpContext 来存储这些信息:

 公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.HttpContext.Items [UnitOfWorkRequestKey] =的UnitOfWork;
}

和则:

 公共覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
{
    VAR的UnitOfWork =(IUnitOfWork)filterContext.HttpContext.Items [UnitOfWorkRequestKey]
    尝试
    {
        如果(filterContext.Exception == NULL)
        {
            unitOfWork.Complete();
        }
    }
    最后
    {
        unitOfWork.Dispose();
        filterContext.Controller.TempData [UnitOfWorkRequestKey] = NULL;
    }
}

好了,现在我们已经在这里固定你的MVC应用程序是如何使用 Request.Properties 集合来实现在Web API中是相同的:

 公共覆盖无效OnActionExecuting(HttpActionContext ActionContext中)
{
    actionContext.Request.Properties [UnitOfWorkRequestKey] =的UnitOfWork;
}

和则:

 公共覆盖无效OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
    VAR的UnitOfWork =(IUnitOfWork)actionExecutedContext.Request.Properties [UnitOfWorkRequestKey]
    尝试
    {
        如果(actionExecutedContext.Exception == NULL)
        {
            unitOfWork.Complete();
        }
    }
    最后
    {
        unitOfWork.Dispose();
    }
}

In my System.Web.Mvc Action filters I previously used TempData to store an instance of my unitOfWork service like so:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.Controller.TempData[UnitOfWorkRequestKey] = UnitOfWork;
    UnitOfWork.Begin();
}

then to commit the transaction I retreived it from temp data like this..

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    var unitOfWork = (IUnitOfWork)filterContext.Controller.TempData[UnitOfWorkRequestKey];
    try
    {
        if (filterContext.Exception == null)
        {
            unitOfWork.Complete();
        }
    }
    finally
    {
        unitOfWork.Dispose();
        filterContext.Controller.TempData[UnitOfWorkRequestKey] = null;
    }
}

So my question is:
In the System.Web.Http Web Api Action Filter (using HttpActionContext) - is there an equivalent location to store my instance of a service, so I can retrieve the same instance when the action has executed?

解决方案

In the System.Web.Http Web Api Action Filter (using HttpActionContext) - is there an equivalent location to store my instance of a service, so I can retrieve the same instance when the action has executed?

No, there isn't. The whole point of an API is that it should be stateless. That's rule number 1. If you need to use Session or TempData in an API you are probably doing something very wrong from a design perspective.

Also you shouldn't be using TempData in your MVC application for this task. TempData is used when you need to persist information between more than one request. In your case it is the same request. So you should have used the HttpContext to store this information:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.HttpContext.Items[UnitOfWorkRequestKey] = UnitOfWork;
}

and then:

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    var unitOfWork = (IUnitOfWork) filterContext.HttpContext.Items[UnitOfWorkRequestKey];
    try
    {
        if (filterContext.Exception == null)
        {
            unitOfWork.Complete();
        }
    }
    finally
    {
        unitOfWork.Dispose();
        filterContext.Controller.TempData[UnitOfWorkRequestKey] = null;
    }
}

Alright, now that we have fixed your MVC application here's how to achieve the same in the Web API using the Request.Properties collection:

public override void OnActionExecuting(HttpActionContext actionContext)
{
    actionContext.Request.Properties[UnitOfWorkRequestKey] = UnitOfWork;
}

and then:

public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
    var unitOfWork = (IUnitOfWork) actionExecutedContext.Request.Properties[UnitOfWorkRequestKey];
    try
    {
        if (actionExecutedContext.Exception == null)
        {
            unitOfWork.Complete();
        }
    }
    finally
    {
        unitOfWork.Dispose();
    }
}

这篇关于网页API行为过滤器 - Controller.TempData相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆