A basic MVC5 controller that does setup for things that should always be done

I am working with an MVC5 project. In this project, I have a few things that I always want to do in almost every response to a client.

For example, I always want to know if a user is logged in, and if so, then the username and their ID in the ViewBag variable is used in the .cshtml file.

I have a base controller that all other controllers inherit from. My first thought was to do these things in the constructor of this controller, but that doesn't work as the variable User

doesn't exist yet.

Is there any other way to do this without calling the Setup () method in every activity? Can I listen to any event that fires before the ActionResult is returned and insert the ViewBag data there?

An example of what doesn't work;)

[InitializeSimpleMembership]
public class BaseController : Controller
{
    protected USDatabase _database = new USDatabase();
    public BaseController()
    {
        if (User.Identity.IsAuthenticated == true)
        {
            var usr = _database.UserProfiles.Where(x => x.UserName.ToLower() == User.Identity.Name.ToLower()).FirstOrDefault();
            if (usr != null)
            {
                ViewBag.UserName = usr.UserName;
                ViewBag.UserId = usr.Id;
            }
        }
    }
}

      

My solution after reading the ideas in the answers below:

Created an ActionFilter that I ran on the base controller.

public class UserDataFilter : ActionFilterAttribute
{
    //OnActionExecuting – This method is called before a controller action is executed.
    //OnActionExecuted – This method is called after a controller action is executed.
    //OnResultExecuting – This method is called before a controller action result is executed.
    //OnResultExecuted – This method is called after a controller action result is executed.

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        var User = filterContext.HttpContext.User;

        if (User.Identity.IsAuthenticated == true)
        {
            using (var db = new USDatabase()) { 
                var usr = db.UserProfiles.Where(x => x.UserName.ToLower() == User.Identity.Name.ToLower()).FirstOrDefault();
                if (usr != null)
                {
                    var ViewBag = filterContext.Controller.ViewBag;

                    ViewBag.UserName = usr.UserName;
                    ViewBag.UserId = usr.Id;
                }
            }
        }
    }
}

      

The base controller now looks like this:

[InitializeSimpleMembership]
[UserDataFilter]
public class BaseController : Controller
{
    protected USDatabase _database = new USDatabase();
    public BaseController()
    {

    }
}

      

And all my other controllers are Implementing the BaseController.

+3


source to share


2 answers


Yes .. you need Action Filters , action filters are .net attributes inherited from ActionFilterAttribute , you can do what you have specified using them, here is a link to understand them and some basic examples of what you do with them: http: //www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs



+2


source


Take a look at http://msdn.microsoft.com/en-us/library/system.web.mvc.controller(v=vs.98).aspx : There are various events that you could use, depending on your specific circumstances ... For example: OnActionExecuting, OnActionExecuted



0


source







All Articles