I'm trying to make a simple chat using asp.net 6 with identity and SignalR.
At the moment I'm trying to connect my razorpage chat with the SignalR hub using HubConnectionBuilder()
. I'm still very much a beginner to Asp.net.
chat.cshtml.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Http.Extensions;
namespace LoginServerChat.Pages
{
public class ChatModel : PageModel
{
public const string HUBURL = "/api/ChatSignal";
private readonly ILogger<ChatModel> _logger;
private readonly UserManager<IdentityUser> _userManager;
private readonly NavigationManager _navigationManager;
private HubConnection _hubConnection;
public List<SelectListItem> Users { get; set; }
public string myUser { get; set; }
public ChatModel(ILogger<ChatModel> logger, UserManager<IdentityUser> userManager, NavigationManager navigationManager)
{
_logger = logger;
_userManager = userManager;
_navigationManager = navigationManager;
_hubConnection = new HubConnectionBuilder().WithUrl(_navigationManager.ToAbsoluteUri(HUBURL)).Build();
}
public void OnGet()
{
Users = _userManager.Users.ToList()
.Select(user => new SelectListItem { Text = user.UserName, Value = user.UserName})
.OrderBy(s => s.Text).ToList();
myUser = User.Identity.Name;
}
}
}
My application stops on line this line _hubConnection = new HubConnectionBuilder().WithUrl(_navigationManager.ToAbsoluteUri(HUBURL)).Build();
with a HttpNavigationManager has not been initialized
error. I'm not really sure what this error means, how it's used and I didn't find an MSDN entry for HttpNavigationManager.
Some tips would be much appreciated!
CodePudding user response:
Problem I had was that I was trying to use Razor pages. Razor pages doesn't have NavigationManager since it's running server side. I have to use Blazor which complies to web assembly and runs client side.