HttpContext.Items与ASP.NET MVC [英] HttpContext.Items with ASP.NET MVC

查看:107
本文介绍了HttpContext.Items与ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我implimenting使用Singleton模式我自己的ApplicationContext类。我想我对它的实例存储在HttpContext.Items,因为它是在请求中的所有部分进行访问。我一直在阅读关于使用的HttpContext的ASP.NET MVC和主要的痛苦之一就是它引入了测试的复杂性。我试过在HttpContext.Items的可测试性做研究,但所有我能找到的东西是在会话。其中一个我发现的唯一的东西是出专业ASP.NET MVC 3.5书Wrox的(一个样章的这里 PDF链接)。第15页就这样说:

I'm implimenting my own ApplicationContext class that uses the singleton pattern. I want to store my instance of it in HttpContext.Items, since it is accessible in all parts of the request. I've been reading about using HttpContext with ASP.NET MVC and one of the major pains is that it introduces testing complexity. I've tried doing research on the testability of HttpContext.Items, but all I can find is stuff on Session. One of the only things I've found is out of a sample chapter in the Professional ASP.NET 3.5 MVC book on Wrox (pdf link here). On page 15 it says this:

是你无法使用:HttpContext.Items

  上述在本节中,我们来了干净,告诉你,我们对你撒谎:HttpContext的是不是ASP.NET MVC和ASP.NET Web窗体之间共享。作为其结果,不能使用HttpContext.Items集合来存储和检索数据的比特点。


  这样做的原因是因为一旦您重定向到一个控制器,你的HttpHandler成为System.Web.Mvc.MvcHandler,这是使用HttpContextWrapper创建的,这将有自己的HttpContext.Current的定义。不幸的是,这种握手期间,像HttpContext.Items不会传输。


  这是什么归结为是,HttpContext的类型,尽管看起来和探空大同小异,都是不一样的,你不能以这种方式传递数据。

Something You Can’t Use: HttpContext.Items
Above in this section, we came clean and told you that we lied to you: HttpContext is not shared between ASP.NET MVC and ASP.NET Web Forms. As a result of this, you cannot use the HttpContext.Items collection to store and retrieve bits of data.

The reason for this is because once you redirect to a Controller, your HttpHandler becomes the System.Web.Mvc.MvcHandler, which is created using HttpContextWrapper, which will have its own definition of HttpContext.Current. Unfortunately, during this handshake, things like HttpContext.Items are not transferred.

What this boils down to is that the HttpContext types, despite looking and sounding very much the same, are not the same, and you cannot pass data in this way.

现在,我试着测试此出来,据我所知,如果你使用RedirectToAction,HttpContext.Items不会保留重定向到另一个控制器。我使用的是默认的ASP.NET MVC项目来测试这一点。我所做的是,这种方法添加到的Global.asax.cs:

Now, I've tried testing this out, and as far as I can tell, if you redirect to another controller using RedirectToAction, HttpContext.Items does remain. I'm using the default ASP.NET MVC project to test this. What I've done is, add this method to Global.asax.cs:

protected void Application_BeginRequest()
{
    Context.Items["Test"] = "Hello World";
}

而在HomeController.cs,我已经改变了指数的方法:

And in HomeController.cs, I've changed the Index method to:

public ActionResult Index()
{
    return RedirectToAction("About");
}

和改变了关于方法:

public ActionResult About()
{
    Response.Write(Convert.ToString(HttpContext.Items["Test"]));
    return View();
}

当我运行应用程序时,页面重定向正确到/ home /关于和Response.Writes正确的Hello World的字符串中的global.asax.cs设置。

When I run the application, the page properly redirects to /Home/About and Response.Writes the correct "Hello World" string set in the global.asax.cs.

所以,在我看来,如果我不是不理解什么书的意思,当他们说
 之​​类的东西HttpContext.Items不会转移OR它传递这个东西,它没关系使用HttpContext.Items。

So, it seems to me as if I'm either not understanding what the book is meaning when they say "things like HttpContext.Items are not transferred" OR it does transfer this stuff and it's okay to use HttpContext.Items.

如果你们推荐我避免HttpContext.Items,有没有存储在每个请求的基础上跨越请求的对象另一种替代方式?

If you guys recommend that I avoid HttpContext.Items, is there another alternative way to store an object across a request on a per-request basis?

推荐答案

你的问题是问一些事情,但我认为项目#1是您正在寻找的答案。

Your question is asking a few things but I think item #1 is the answer you're looking for.


  1. 它是精细使用 Context.Items 缓存在每个请求的基础?
    是。如果在过程中,每个请求,每台机器在网络农场是您的条件然后Context.Items给你。

  1. Is it fine to use Context.Items for caching on a per request basis? Yes. If in process, per request, per machine in the web farm is your criteria then Context.Items gives you that.

Context.Items 难以测试?
至于可测试性,我会隐藏 Context.Items 某种类型的接口后面。这样,你得到的单元测试功能,而无需直接引用 Context.Items。否则,你需要什么测试约 Context.Items ?该框架将存储和检索值?保持你的code无知的System.Web ,你就会成为一个快乐的人。

Is Context.Items difficult to test with? As far as testability, I would hide Context.Items behind an interface of some sort. This way you get unit testing capabilities without having to reference Context.Items directly. Otherwise, what do you need to test about Context.Items? That the framework will store and retrieve values? Keep your code ignorant of System.Web and you'll be a happy camper.

威尔 Context.Items 生存 RedirectToAction
号您的测试是无效的。它设置在每个Web请求你好,世界和您的测试跨越两个web请求。第一是当Index操作被调用。二是当 RedirectToAction 动作叫(这是一个HTTP 302)。为了使其失败,在索引操作设置一个新值,看它是否保留在关于行动。

Will Context.Items survive RedirectToAction? No. Your test is invalid. It's setting "Hello, world" on every web request and your test spans two web requests. The first is when the Index action is called. The second is when RedirectToAction action is called (it's an HTTP 302). To make it fail, set a new value in the Index action and see if it's retained in the About action.

这篇关于HttpContext.Items与ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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