2

First off all i'm fairly new to C# and ASP.NET (mainly program java).

I've got a BaseController where i want to fill a viewbag for all my other controllers to use. I also set some session data and create some cookies. This is the basecontroller:

    public abstract partial class BaseController : Controller
{
    // GET: Base
    protected override void Initialize(RequestContext requestContext)
    {
        try
        {
            //Checks if the user is logged in
            if (requestContext.HttpContext.Session["customer"] != null)
            {
                ViewBag.Customer = requestContext.HttpContext.Session["customer"];
                ViewBag.Points = requestContext.HttpContext.Session["points"];
                ViewBag.CardNumber = requestContext.HttpContext.Session["cardNumber"];
            }

            //Gets the products to be displayed
            var products = ProductList.Instance.AsQueryable();
            ViewBag.Products = products;

            //Checks to see if the user has a cart added to his requestContext.HttpContext.Session
            if (requestContext.HttpContext.Session["cart"] == null)
            {
                requestContext.HttpContext.Session["cart"] = new Cart();
            }
            Cart cart = (Cart)requestContext.HttpContext.Session["cart"];

            ViewBag.CartCount = cart.Count();
            if (requestContext.HttpContext.Session["ticketID"] == null)
            {
                requestContext.HttpContext.Session["ticketID"] = Guid.NewGuid();
            }

            //Adds a cookie to the user with his selected theme
            HttpCookie cookieUserTheme = requestContext.HttpContext.Request.Cookies["cookieUserTheme"];
            if (cookieUserTheme != null)
            {
                requestContext.HttpContext.Session["UserPref"] = UserModel.GetThemeByName(cookieUserTheme.Value);
            }
            else
            {
                requestContext.HttpContext.Session["UserPref"] = UserModel.GetThemeByName("5");
                var cookie = new HttpCookie("cookieUserTheme", ((UserPref)requestContext.HttpContext.Session["UserPref"]).ID);
                cookie.Expires = DateTime.Now.AddDays(90);
                requestContext.HttpContext.Response.Cookies.Add(cookie);
            }
            ViewBag.UserPref = requestContext.HttpContext.Session["UserPref"];
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
}
}

And this is the controller.

    public class AdminController : BaseController
{
    // GET: Admin
    public ActionResult Index()
    {
        ViewBag.Themes = SiteMethods.GetAllThemes();
        return View();
    }

But when this is done running it just goes to the following ASP.NET page enter image description here

What am i doing wrong? Do i need a redirect from my basecontroller?

1 Answer 1

3

If you're overriding Controller.Initialize() with your own initialization logic, you need to call base.Initialize(requestContext) to continue with the regular initialization process:

protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);

    // rest of your code
    // ...
}

Otherwise, this.ControllerContext (that is being used internally by several properties), would be null.

See Source

Sign up to request clarification or add additional context in comments.

1 Comment

Oke i missed this! It's wierd that this is not easier to find. But it works now thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.