-3

I want to be able to display the database name from the dbContext. I have tried to pass it as a ViewBag but when i change page it disappears from the footer.

when I try pass it as a view model the application crashes when i change page.

private readonly DBContext _context;

public HomeController(DBContext context)
{
    _context = context;
}

public IActionResult Index()
{
    var dbString = _context.Database.GetDbConnection().Database; 

    return View(new BaseViewModel { DatabaseName = dbString });
}

my BaseViewModel class is just a string property with DatabaseName

then in my footer i have

@using Project.Models.ViewModels;
@model BaseViewModel;
<footer class="app-footer">
    <div class="ml-auto">
        <span style="font-weight: bold;">
           @Model.DatabaseName 
        </span>
    </div>
</footer>

What is the best way to achieve this? I have also have @using Project.Models.ViewModels and @model BaseViewModel in my layout page.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You haven't explained why it crashes, which by the way - will be because the DatabaseName and/or Model isn't being populated on every controller action. A simple search would have brought you to the following question - https://stackoverflow.com/questions/13225315/pass-data-to-layout-that-are-common-to-all-pages – ColinM Jun 13 '18 at 09:26
  • Ive already followed that... When i change page the Name disappears from the Footer..... Its only there when i start the app, when i change page its gone... I inherited the base to all view models –  Jun 13 '18 at 09:35
  • You've inherited base, but you need to set the database name in each controller action, or find another means for populating the database name and model. Otherwise provide more information in the answer, as right now there isn't really anything helpful to go by - just "I am doing this, and I have a problem" – ColinM Jun 13 '18 at 09:46
  • thats stupid putting the db name in every controller. That's why i asked for a better way. –  Jun 13 '18 at 09:53
  • Use the application state for storing the database name. Store the database name in application state at Application_Start() method of global.asax and the you can use it anywhere in application using HttpContext.Current.Application["DbName"] similar to sessions. – Ankit Sahrawat Jun 13 '18 at 10:01
  • 2
    is this a good idea from a security point of view? Perhaps you should use some user-friendly name to identify the DB rather than the actual DB name as it appears in SQL Server. As a rule you shouldn't give info about the system away on the web page - anything that could potentially help a hacker should be a secret – ADyson Jun 13 '18 at 10:13
  • Post nonsense questions, get nonsense comments Karl ;-) – ColinM Jun 13 '18 at 11:13

1 Answers1

1

It can be done by multiple ways: 1. You can store the database name in application state. In the global.asax/startup file's Application_Start() method you can call a method to set DbName in Application State like below:

protected void Application_Start()
{
    System.Web.HttpContext.Current.Application.Lock();
    System.Web.HttpContext.Current.Application["DbName"] = //call method to set db name;
    System.Web.HttpContext.Current.Application.UnLock();
}

It can be used anywhere in application using System.Web.HttpContext.Current.Application["DbName"]

  1. Use partial views with Html.RenderAction:

Create a BaseController and one action method in it. In this action method get the database name as above and return a partial view from it. In that partial view write html to return a simple string as database name

Use OutputCache and ChildActionAttribute attributes on this action method to cache database name and to use it as partial view only.

Now in your layout page render this partial view using @{Html.RenderAction("actionName", new { controller="name" } );} in place of @Model.DatabaseName in your footer

Ankit Sahrawat
  • 1,306
  • 1
  • 11
  • 24