Why do I get 500 error on Azure after using ViewBag?

52 Views Asked by At

I've been testing my app on localhost and it works fine. But, when I tried to deploy it after changing this code, it didn't work anymore. Azure thows me 500 error for no reason at all.

I added this base controller and each controller inherits from it:

    public class BaseController(IConfiguration configuration) : Controller
    {
        private readonly IConfiguration _configuration = configuration;
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                if (_configuration["BlockPage"] == "true")
                {
                    filterContext.Result = new ViewResult { ViewName = "~/Views/PageBlocked.cshtml" };
                    return;
                }
                ViewBag.PaidPage = _configuration["PaidPage"] == "true" ? true : false;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

For instance:

public class HomeController(IWorkContainer workContainer, SignInManager<ApplicationUser> signInManager, IConfiguration configuration) : BaseController(configuration)
{
    private readonly IWorkContainer _workContainer = workContainer;
    private readonly SignInManager<ApplicationUser> _signInManager = signInManager;
...
...
...
}

The only place I use ViewBag is in _Layout.cshtml to show the user a message:

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@{
    var paidPage = ViewBag.PaidPage;
}


<!DOCTYPE html>
...
...
...

Almost every .cshtml View uses this layout.

I suspect it comes from ViewBag, because after several changes, it was the only thing I removed that made the page work again. Still don't understand why, need some explanation and if possible also a fix please!

0

There are 0 best solutions below