Search code examples
c#asp.net-corerazor-pagesasp.net-core-2.2

Error when assigning value to ViewData in PageFilter


I'm working on a PageFilter, that I can call on each page to get some data for my _Layout page:

public class GetEventFilter : IAsyncPageFilter
{
    private readonly DBContext _context;

    public GetEventFilter(DBContext context)
    {
        _context = context;
    }

    public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
    {

        if (context.HandlerInstance is PageModel result)
        {

            //result.Response.StatusCode = 400;
            context.Result = result.Page();

            DateTime dt = DateTime.Now;

            var thisEvent = await _context.Events.Include(x => x.AdminCompany).Where(x => x.Domain == context.HttpContext.Request.Host.Host && x.Status == 1 && x.ActiveFromDateTime <= dt && x.ActiveToDateTime > dt).OrderBy(x => x.ActiveFromDateTime).FirstOrDefaultAsync();

            if (thisEvent != null)
            {
                if (thisEvent.Logo != null)
                {
                    result.ViewData["Site_EventLogo"] = "/data/" + thisEvent.AdminCompany.Id + "/" + thisEvent.Id + "/media/" + thisEvent.Logo;
                }
                else
                {
                    result.ViewData["Site_EventLogo"] = "/data/" + thisEvent.AdminCompany.Id + "/_system/media/" + thisEvent.AdminCompany.Logo;
                }
                result.ViewData["Site_EventId"] = thisEvent.Id.ToString();
                result.ViewData["Site_EventName"] = thisEvent.Name;
                result.ViewData["Site_EventDomain"] = thisEvent.Domain;
                result.ViewData["Site_CompanyId"] = thisEvent.AdminCompany.Id;
                result.ViewData["Site_CompanyName"] = thisEvent.AdminCompany.Name;
                result.ViewData["Site_CompanyAddress"] = thisEvent.AdminCompany.Address;
                result.ViewData["Site_CompanyPostalCode"] = thisEvent.AdminCompany.PostalCode;
                result.ViewData["Site_CompanyCity"] = thisEvent.AdminCompany.City;
                result.ViewData["Site_CompanyPhone"] = thisEvent.AdminCompany.Phone;
                result.ViewData["Site_CompanyEmail"] = thisEvent.AdminCompany.Email;
                //await next.Invoke();
            }
            else
            {
                context.HttpContext.Response.Redirect("/error/404");
            }

        }

    }

    public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) => await Task.CompletedTask;

}

My problem is that when I assign values to the ViewData object I get an NullReferenceException: Object reference not set to an instance of an object.-error.

How can I fix that? I do know what a NullReferenceException is, I just don't know why I get it in this example.


Solution

  • After a discussion on this problem, the first step was to figure out why the PageResult was always returning null. The answer to this was found here. After resolving the first issue, it was found that on invoking await next.Invoke();, the following error was encountered: If an IAsyncPageFilter provides a result value by setting the Result property of PageHandlerExecutingContext to a non-null value, then it cannot call the next filter by invoking PageHandlerExecutionDelegate.

    To resolve this error, the assignement of the contex result: context.Result = result.Page(); was removed and then the task was invoked again which resolved the entire issue.