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?
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.
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.
ViewData is a dictionary object and it is property of ControllerBase class. ViewData is faster than ViewBag .
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With