How to access the HttpServerUtility.MapPath method in a Thread or Timer?

It’s possible to use HostingEnvironment.MapPath() instead of HttpContext.Current.Server.MapPath() I haven’t tried it yet in a thread or timer event though. Some (non viable) solutions I considered; The only method I care about on HttpServerUtility is MapPath. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory and build my paths from this. But this will fail if … Read more

How do I get an HttpContext object from HttpContextBase in ASP.NET MVC 1?

The simplest way is to get the application, ApplicationInstance, and use its Context property: // httpContextBase is of type HttpContextBase HttpContext context = httpContextBase.ApplicationInstance.Context; (thanks to Ishmael Smyrnow who noted this in the comments) Original answer: You can, especially if the HttpContextBase instance you’ve been handed is of type HttpContextWrapper at run-time. The following example … Read more

Mock HttpContext.Current in Test Init Method

HttpContext.Current returns an instance of System.Web.HttpContext, which does not extend System.Web.HttpContextBase. HttpContextBase was added later to address HttpContext being difficult to mock. The two classes are basically unrelated (HttpContextWrapper is used as an adapter between them). Fortunately, HttpContext itself is fakeable just enough for you do replace the IPrincipal (User) and IIdentity. The following code … Read more

Setting HttpContext.Current.Session in a unit test

You can “fake it” by creating a new HttpContext like this: http://www.necronet.org/archive/2010/07/28/unit-testing-code-that-uses-httpcontext-current-session.aspx I’ve taken that code and put it on an static helper class like so: public static HttpContext FakeHttpContext() { var httpRequest = new HttpRequest(“”, “http://example.com/”, “”); var stringWriter = new StringWriter(); var httpResponse = new HttpResponse(stringWriter); var httpContext = new HttpContext(httpRequest, httpResponse); var … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)