aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Question: A view component must return a non-null value?

Open nj-sketch opened this issue 9 years ago • 1 comments

 fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
      An unhandled exception has occurred while executing the request
System.InvalidOperationException: A view component must return a non-null value.

I get this error even when my ViewComponent is not present on the layout. I have two layouts _FELayout.cshtml and _Layout.cshtml. I have my ViewComponent invoked from _Layout.cshtml. But I get this error while running on debugger mode when I am in _FELayout.cshtml layout. Though, it doesn't affect the view of the application. Still curious why I am getting this error.

.NET Core version: 2.0

nj-sketch avatar Nov 02 '17 05:11 nj-sketch

Here is my ViewComponent Code

 public class LoginStatusViewComponent : ViewComponent
    {
        private readonly SignInManager<User> _signInManager;
        private readonly UserManager<User> _userManager;

        public LoginStatusViewComponent(SignInManager<User> signInManager, UserManager<User> userManager)
        {
            _signInManager = signInManager;
            _userManager = userManager;
        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            if (_signInManager.IsSignedIn(HttpContext.User))
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);
                return View("LoggedIn", user);
            }
            else
            {
                return View();
            }
        }
    }

Here is how it is invoked!

<vc:login-status></vc:login-status>

nj-sketch avatar Nov 02 '17 05:11 nj-sketch