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 8233771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:14:41+00:00 2026-06-07T18:14:41+00:00

I currently reference HttpContext in my models. Is this good practice? Should I just

  • 0

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") { ...}
}
  • 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-06-07T18:14:43+00:00Added an answer on June 7, 2026 at 6:14 pm

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Android, I want to pass the reference of currently running MediaPlayer to another
How should I correctly reference protocols on a UML class diagram? For example my
I reference the current page in a class with Page page = HttpContext.Current.CurrentHandler as
I currently have a Javascript function that uses a string to reference an object
I'm currently in the middle of developing an iPhone app with a big reference
I've got an open source nuget package AttributeRouting . Currently the packaged assemblies reference
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
I'm referencing the article reference To create a UserControl dynamically and pass it back
string htmlEncode = HttpContext.Current.Server.HtmlEncode(value); return Object reference not set to an instance of an
Say I'm working with Sharepoint (this applies to other object models as well) and

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.