0

I have the below Controller class

public class HomeController : Controller { public ActionResult Index(CustomerModel model) { Validate.ValidateModel(model); HttpContext.Application["IsAppIdAvailable"]; return View(); } } 

and I have a Validate.cs class file like below

public class Validate { public static void Test() { if(Convert.ToBoolean(HttpContext.Application["IsAppIdAvailable"])) { //Just assume this senario return true; } } } 

In the test method of the Validate class we are not able to access the HttpContext.Application but we are able to access the HttpContext.Application in the HomeController.

I understand the HomeController inherit from Controller class because of that HttpContext.Application has value.

Is there anyway I can access the HttpContext.Application in a class which does not inherit Contoller class. Please help

2
  • Are you using dependency injection? Try to stay away from coupling your code to HttpContext directly. Abstract away the functionality you want and inject that into your controller Commented Mar 1, 2017 at 3:22
  • check this answer and see if it is of use to you and your situation stackoverflow.com/questions/33440653/… Commented Mar 1, 2017 at 3:26

2 Answers 2

1

Use static property of System.Web.HttpContext class

System.Web.HttpContext.Current.Application

Remember, that System.Web.HttpContext.Current is available only in request-handling threads.

Update

Ah, mocking. Of course.

HttpContextWrapper

Sign up to request clarification or add additional context in comments.

1 Comment

if we use System.Web.HttpContext.Current in the controller class then we can't mock the HttpConted when we do the NUnit testing. So I need to use HttpContext.Application in the Controller and need to access the same in the Validate class also.How?
0

Hi use this one: HttpContext is a class name, it is a static method

HttpContext.Current.Application 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.