Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Visual Studio 2010 mixing up System.Web And System.Web.Abstractions?

Visual Studio 2010 seems to be mixing up the above mentioned libraries.

This code sample is from the book "Pro ASP.NET MVC2 Framework" by Steven Sanderson.

[TestMethod]
public void HomePage_Recognizes_New_Visitor_And_Sets_Cookie() 
{
    // Arrange: First prepare some mock context objects
    var mockContext = new Mock<HttpContextBase>();
    var mockRequest = new Mock<HttpRequestBase>();
    var mockResponse = new Mock<HttpResponseBase>();

    // The following lines define associations between the different mock objects
    // (i.e. tells Moq what alue to use for tMockContext.Request)
    mockContext.Setup(x=> x.Request).Returns(mockRequest.Object);
    mockContext.Setup(x=> x.Response).Returns(mockResponse.Object);
    mockRequest.Setup(x=> x.Cookies).Returns(new HttpCookieCollection());
    mockResponse.Setup(x=> x.Cookies).Returns(new HttpCookieCollection());

    var homeController = new HomeController();
    var requestContext = new RequestContext(mockContext.Object, new RouteData());
    homeController.ControllerContext = new ControllerContext(requestContext, homeController);

    // Act
    ViewResult viewResult = homeController.HomePage();

    // Assert
    Assert.AreEqual(String.Empty, viewResult.ViewName);
    Assert.IsTrue((bool)viewResult.ViewData["IsFirstVisit"]);
    Assert.AreEqual(1, homeController.Response.Cookies.Count);
    Assert.AreEqual(bool.TrueString, homeController.Response.Cookies["HasVisitedBefore"].Value);
}

My project references the System.Web and System.Web.Abstractions libraries.

When the code file is only "using System.Web" I get two errors:

  1. (Line 25 under the word 'Assert') The type 'System.Web.HttpResponseBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
  2. (Lines 25 and 26 under the word 'Cookies') 'System.Web.HttpResponseBase' does not contain a definition for 'Cookies' and no extension method 'Cookies' accepting a first argument of type 'System.Web.HttpResponseBase' could be found (are you missing a using directive or an assembly reference?)

If I add "using System.Web.Abstractions" to the code file and build the project, the above errors go away but then then I get the following error:

  1. The type or namespace name 'Abstractions' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

Interestingly, in both cases when I place a dot after Response, Intellisense prompts me with the correct choices (i.e. Response.Cookies). It seems like Intellisense has information about HttpResponseBase that the build engine does not.

Any idea what may be causing this?

like image 720
Jason Avatar asked Sep 30 '10 21:09

Jason


1 Answers

Can you double check that under your Project References you have both System.Web and System.Web.Abstractions?

Using the source code for the book downloaded from the Apress site, I can build your code without errors, but when I remove the reference to System.Web.Abstractions, I get the same behavior, including the intellisense information prompts you noted.

like image 138
Jeff Ogata Avatar answered Nov 15 '22 09:11

Jeff Ogata