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

  • Home
  • SEARCH
  • 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 8596673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:44:06+00:00 2026-06-12T00:44:06+00:00

I need to apply filtering by requesting host name on all database calls in

  • 0

I need to apply filtering by requesting host name on all database calls in my Web API service.

This filtering works like so:

  1. lookup the Site to profile against based on the requesting hostname
  2. Apply Site.Id on all subsequent data access calls made in the request

Essentially a global filter so so that data returned by the API service is always contained to the host.

One solution would be to pass the host name in as an argument on all my service methods like so:

public IEnumerable<Profiles> GetProfiles ()
{
 var host = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
 return profilesService.Get(host);
}

But since this is a consistent rule on all requests I would like to come up with a more elegant way to handle this so my service calls are just profileSerivce.Get();

I think I need to inject a ISiteLocator into my service layer that has either the host name or even better the Id already retrieved from the database that I can then apply. But I’m struggling with this on how and where I can reference the HttpContext to get the host name and also if it was possible to optimise this using StructureMap lifecycles.

  • 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-12T00:44:07+00:00Added an answer on June 12, 2026 at 12:44 am

    I think I need to inject a ISiteLocator into my service layer

    It seems to me you are heading into the right direction.

    I’m struggling with this on how and where I can reference the
    HttpContext

    This is really simple actually. Define the ISiteLocator in your business layer and define an AspNetSiteLocator implementation into your ASP.NET web application, preferably close to (or inside) your Composition Root. That implementation might look like this:

    public class AspNetSiteLocator : ISiteLocator
    {
        private readonly ISiteRepository siteRepository;
    
        public AspNetSiteLocator(ISiteRepository siteRepository)
        {
            this.siteRepository = siteRepository;
        }
    
        Site ISiteLocator.GetCurrentSite()
        {
            return this.siteRepository.GetById(CurrentHostName);
        }
    
        private static string CurrentHostName
        {
            get
            {
               return HttpContext.Current.Request
                   .ServerVariables["SERVER_NAME"];
            }
        }
    }
    

    that has either the host name or even better the Id already retrieved
    from the database

    Try to let your ISiteLocator return data in a way that is most convenient to the consumers of that locator. In my example I returned a Site entity (if you have such an entity in your domain). This is probably more convenient than the host name or the Id, since consumers possibly have to query for the site again thereselves. However, perhaps Id is the most convenient, but that’s up to you to find out.

    how […] to optimise this using StructureMap lifecycles

    The implementation above doesn’t have any state, so it can be registered with any lifetime; singleton for instance. However, every call to ISiteLocator.GetCurrentSite() will result in a new request to the ISiteRepository, which can cause too much overhead. In that case you probably want an implementation that stores the Site in a private field and always returns that instance. In that case you shoud register that implementation on a ‘Per Web Request’ basis (since the SERVER_NAME will not change during the request).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I need to apply a custom message formatter to all operations within a service.
I need to apply a css class to invalid controls on a web form.
I need to apply this to images that are selected by jQuery selector for
I have this code I need apply a theme to a Cacel Button present
I need to apply some function to all elements of JSON object. I guess
I need to apply a minify actions to all the javascript and CSS files,
I need to apply this script to work the same way for two other
I'm using asp.net website and I need to apply css style to all text
I need to apply a style in code ike this: TextBlock.Style = TryFindResource(MyStyle) as

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.