D
D
Dmitry Filandor2016-05-25 21:59:21
ASP.NET
Dmitry Filandor, 2016-05-25 21:59:21

Why doesn't Base Controller work?

Hello!
There are two tasks:
1) to store the user object in the session so that you do not make queries to the database for its properties. (There are quite a lot of users on the site);
2) using the base controller to catch errors of all controllers
There is asp mvc 5 VS 2015
What I do:
Base controller:

namespace WEB.Controllers
{
    public class BaseController : Controller
    {


        private BusinessLayer.User.User _currentUser;
        public BusinessLayer.User.User CurrentUser
        {
            get
            {
                if (!Request.IsAuthenticated) return null;

                if (_currentUser != null) return _currentUser;

                //try to get it form Session first, if its not there -> create it and put in the session
                if (Session["CurrentUser"] == null)
                {
                    _currentUser = new UserManager().UserDetailed(User.Identity.Name);
                    
                    Session["CurrentUser"] = _currentUser;
                    return _currentUser;
                }
                else //set it to the local var for the multiple references in the calling code and return
                {
                    _currentUser = (BusinessLayer.User.User)Session["CurrentUser"];
                    return _currentUser;
                }
            }

            set
            {
                _currentUser = value;
                Session["CurrentUser"] = _currentUser;
            }
        }



    }
}

In Global.asax I catch all errors:
protected void Application_Error(object sender, EventArgs e)
        {
            BusinessLayer.Extensions.Mailer mailerManager = new BusinessLayer.Extensions.Mailer();

            HttpContext ctx = HttpContext.Current;
            Exception ex = ctx.Server.GetLastError();
            ctx.Response.Clear();

            RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
            IController controller = new  WEB.Controllers.BaseController();  // Тут можно использовать любой контроллер, например тот что используется в качестве базового типа
            var context = new ControllerContext(rc, (ControllerBase)controller);

            var viewResult = new ViewResult();

            var httpException = ex as HttpException;
            if (httpException != null)
            {

               


                switch (httpException.GetHttpCode())
                {


                    case 404:
......

Then any controller, for example:
...

namespace WEB.Controllers
{

    
    public class EventsController : BaseController
    {


 public ActionResult UserStream()
        {
            if (User.Identity.IsAuthenticated)
            {
                BusinessLayer.User.UserManager UM = new BusinessLayer.User.UserManager();
                ViewBag.EventsMain = UM.UserStream(CurrentUser.Id, 20);
                return View("UserStream");
            }            
            else { return View("UserStreamNA"); }            
        }
......

CurrentUser - NULL. I set a breakpoint in the base controller, but the process does not go there and of course CurrentUser is not initialized ....
What did I miss?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Malinovsky, 2016-05-26
@GAVRAN

Perhaps this is the reason

...the type of the session variable must be a .NET type or a serializable type...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question