Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Centralizing repeating VIewData in MVC

When a user log in into my application i want to show his name throughout the whole application. I am using the asp.net MVC framework. But what i don't want is that is have to put in every controller something like:

ViewData["User"] = Session["User"];

This because you may not repeat yourself. (I believe this is the DRY [Don't Repeat Yourself] principle of OO programming.) The ViewData["User"] is on my masterpage. So my question is, what is a neat way to handle my ViewData["User"] on one place?

like image 883
Martijn Avatar asked Apr 17 '09 08:04

Martijn


2 Answers

You can do this fairly easily in either a controller base-class, or an action-filter that is applied to the controllers/actions. In either case, you get the chance to touch the request before (or after) the action does - so you can add this functionality there.

For example:

public class UserInfoAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        filterContext.Controller.ViewData["user"] = "Foo";
    }
}
...
[HandleError, UserInfo]
public class HomeController : Controller
{...}

(can also be used at the action (method) level)


or with a common base-class:

public abstract class ControllerBase : Controller
{
    protected override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        ViewData["user"] = "Bar";
        base.OnActionExecuting(filterContext);
    }
}

[HandleError]
public class HomeController : ControllerBase
{...}
like image 116
Marc Gravell Avatar answered Sep 27 '22 16:09

Marc Gravell


It's been a year, but I've just stumbled across this question and I believe there's a better answer.

Jimmy Bogard describes the solution described in the accepted answer as an anti-pattern and offers a better solution involving RenderAction: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/18/the-filter-viewdata-anti-pattern.aspx

like image 30
Paul Suart Avatar answered Sep 27 '22 18:09

Paul Suart