Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass ViewData to all actions without having to write it every time

All, I am simply trying to read a custom value from a loaded .config file into an MVC project.

So I set a value such as the below in my added .config file:

"AppSettings": {
    "Title": "Mobile Site"
  }

I've made a base controller to set the values

public abstract class ControllerBase : Controller
{
    protected Models.AppSettings _appSettings;
    private IConfiguration _config;
    public ControllerBase(IConfiguration config)
    {
        _config = config;
        _appSettings = new Models.AppSettings();
        _appSettings.SiteTitle = _config.GetSection("AppSettings").GetSection("Title").Value;

        ViewBag.Settings = _appSettings;
    }
}

This I can also confirm is working. The values are pulled through if I simply set the viewbag data in one of my actions.

public class AppController : ControllerBase
{
    public AppController(IConfiguration config) : base(config)
    {

    }
    // GET: /<controller>/
    public IActionResult Index()
    {
        ViewData["Settings"] = _appSettings;
        return View("Index");
    }

I can access the values in the view this way, but as the values I'm currenlty adding are simply what the site title is (so I can config these things in a simple .config file) I want stuff like the site title to be passed to every action without having to specifically pass it each and every time for every single action.

I tried following a video showing this working by settings this in the view:

@{
    ViewData["Logo"] = "~/img/bird.png";
    ViewData["Icon"] = "~/img/logo.ico";
    var Settings = ((Mobile.Models.AppSettings)ViewBag.Settings);
}

But the page will fail to load properly if again, I dont specify the settings within the action loading the page.

How can this be done? Can this be done any more?

like image 948
Monolithcode Avatar asked May 18 '16 23:05

Monolithcode


People also ask

How do I pass a ViewData model?

To pass the strongly-typed data from Controller to View using ViewData, we have to make a model class then populate its properties with some data and then pass that data to ViewData dictionary as Value and selecting Key's name is the programmer's choice.

Is typecasting required in ViewData?

ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys. ViewData requires typecasting for complex data type and check for null values to avoid error. ViewBag doesn't require typecasting for complex data type.

What is better ViewData or ViewBag?

ViewData is a dictionary object and it is property of ControllerBase class. ViewData is faster than ViewBag .

What is difference between TempData and ViewData?

In one sentence: TempData are like ViewData with one difference: They only contain data between two successive requests, after that they are destroyed. You can use TempData to pass error messages or something similar.


1 Answers

MVC Core approach to implementing aspect-oriented functionality is the same as in MVC 5. There are at least two ways to inject a code before/after each action:

А. Override OnActionExecuting in your ControllerBase:

public override void OnActionExecuting(ActionExecutingContext context)
{
    ViewBag.foo = "1";
}

OR

B. Add ActionFilter to the controller (the filter has to be registered in DI):

public class TestFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        ViewBag.foo = "1";
        base.OnActionExecuting(context);
    }
}

[ServiceFilter(typeof(TestFilter))]
public class TestController : Controller
{
}

More examples: Asp.net Core 1.0 Action Filters. The second way is more flexible (for example, you can easily add the filter to a particular action).

like image 61
Ilya Chumakov Avatar answered Sep 17 '22 20:09

Ilya Chumakov