I currently reference HttpContext in my models. Is this good practice? Should I just pass in information I need instead?
For example, in my model code:
public void function()
{
string param = HttpContext.Current.Request.QueryString["param"];
if(param == "myexpectations") { ...}
}
Should be changed to:
public void function(string param) //avoid hard reference this way??
{
if(param == "myexpectations") { ...}
}
It is not good practice to reference
HttpContextin your models. You don’t want that kind of tight coupling between your model andHttpContext. 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: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 paramaterparam.