It is not good practice to reference HttpContext
in your models. You don't want that kind of tight coupling between your model and HttpContext
. It will make your model very difficult to test, among other things. I would definitely go with your second option.
If you are retrieving your query string values in an action method, you don't need to use HttpContext.Current.Request.QueryString
. You can allow ASP.NET's binding mechanism to bind the query string values to parameters in your action method. E.g. If this is your URI:
http://localhost/Home/TestQueryString?param=ThisIsATestValue
Assuming you have your routes set up correctly, you can create your controller and action like this and MVC will bind the query string value "ThisIsATestValue"
to the paramater param
.
public class HomeController : Controller
{
public ActionResult TestQueryString(string param)
{
string fromHttpContext = HttpContext.Current.Request.QueryString["param"];
// result will be set to true
bool result = param == fromHttpContext;
return new EmptyResult();
}
}