Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1957944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:57:07+00:00 2026-05-17T07:57:07+00:00

I am working with a faked HttpContext (code provided in the end) and probably

  • 0

I am working with a faked HttpContext (code provided in the end) and probably I am missing something because I can’t access TempData collection (forth line of SetFakeControllerContext method). Every time I try I get this error message:

‘controller.TempData’ threw an exception of type ‘System.AccessViolationException’

The code that calls FakeHttpContext is:

    public static void SetFakeControllerContext(this Controller controller)
    {
        HttpContextBase httpContext = FakeHttpContext(string.Empty);
        var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
        controller.ControllerContext = context;
        controller.TempData = new TempDataDictionary(); //This is not necessary! It was just a test!!!!
    }

Does anybody know what I am doing wrong?

public static HttpContextBase FakeHttpContext(string username){

var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();

context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
context.Setup(ctx => ctx.User).Returns(user.Object);

user.Setup(ctx => ctx.Identity).Returns(identity.Object);

if (!string.IsNullOrEmpty(username))
{
    identity.Setup(id => id.IsAuthenticated).Returns(true);
    identity.Setup(id => id.Name).Returns(username);
}
else
{
    identity.Setup(id => id.IsAuthenticated).Returns(false);
    identity.Setup(id => id.Name).Returns(string.Empty);
}

context.Setup(ctx => ctx.Response.Cache).Returns(CreateCachePolicy());
return context.Object;

}

P.s.: I am using Moq

UPDATE:

OMG!! I CAN’T BELIEVE IT! More than 2 hours to figure out that the problem was a reference to the wrong MVC dll. I was referencing System.Web.Mvc 2.0 for my main app but System.Web.Mvc 1.0 in another project. Sorry about this!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T07:57:07+00:00Added an answer on May 17, 2026 at 7:57 am

    The problem’s definitely somewhere else. You don’t even need to initialize the TempData property to a new dictionary in your unit test. The following program works fine:

    public class HomeController: Controller
    {
        public ActionResult Index()
        {
            TempData["foo"] = "bar";
            return View();
        }
    }
    
    class Program
    {
        static void Main()
        {
            var controller = new HomeController();
            controller.Index();
            Console.WriteLine(controller.TempData["foo"]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with TCL and I'd like to implement something like the Strategy Pattern .
I'm faced with writing a framework to simplify working with a large and complex
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project at the moment and we have to implement soft deletion
Working on a somewhat complex page for configuring customers at work. The setup is
Working on a project that parses a log of events, and then updates a
Working with python interactively, it's sometimes necessary to display a result which is some
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.