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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:19:39+00:00 2026-06-17T22:19:39+00:00

I appologize if this is a possible duplication, I’ve looked everywhere and couldn’t find

  • 0

I appologize if this is a possible duplication, I’ve looked everywhere and couldn’t find an answer.

My question is more of a “best practice/convention”-sort of question.

So here is my situation, I’m having an AccountController -> something like this:

    public AccountController(IAuthenticationHelper authHelper, 
                             IAccountService accountService)
    {
        _authHelper     = authHelper;
        _accountService = accountService;
    }

In my _Layout view, I have a placeholder for the currently logged in account. In order to get the currently logged in account, I’m retrieving the current user identity from the HttpContext (which I have in a wrapper class so I can unit test it) -> then I’m getting the account details from the DB.

Now here is my question, I need this data In the _Layout, I could possibly do a partial view expecting an account model -> place it in the _Layout … And here is where I get stuck, I don’t like the idea of so many trips to the database, and I don’t like the fact that I have to think about this small detail from within all Actions ? Am I missing something here, am I thinking of this wrong ? Did I got the concept wrong ? What’s the right way to do this ? (In a testable manner, preferably).

Help is much appreciated !

EDIT: I’ll be happy to provide with more code, if required.

  • 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-17T22:19:40+00:00Added an answer on June 17, 2026 at 10:19 pm

    You could use Child actions to achieve that. For example you will have a specific controller dedicated to retrieving the currently logged user details and pass them to a partial view:

    public class UserInfoController: Controller
    {
        [ChildActionOnly]
        public ActionResult Index()
        {
            var model = new UserDetailsViewModel();
            if (User.Identity.IsAuthenticated)
            {
                string username = User.Identity.Name;
                // fetch the user info from the database and populate your view model
                // consider caching this information to avoid multiple round-trips 
                // to the database
            }
    
            return PartialView(model);
        }
    }
    

    Then of course you will have a corresponding partial view to display the user information.

    And from your _Layout you could render this child action:

    @Html.Action("Index", "UserInfo")
    

    And of course to avoid multiple roundtrips to the database you could simple cache this information.

    Now your main actions and models don’t have to worry about this common functionality. It is handled by the child action. Obviously you have the possibility to use Dependency Injection in the UserInfoController to make it perfectly unit testable.

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

Sidebar

Related Questions

I apologize if this is a duplicate post, but I couldn't find the answer
I apologize if this is a duplicate question, I searched a bit and couldn't
I apologize if this is a duh question. It seems like the answer should
Note: Let me appologize for the length of this question, i had to put
I apologize if this question was asked already before but I could not find
I will try to make my question sound as unconfusing as possible. I appologize
I appologize for this lengthy post. I have made it as small as possible
I'll try to explain this as best as i can, but i appologize if
I apologize if this is a simple question but I've been unable to find
It's possible this question exists elsewhere, and if so, I apologize. After searching for

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.