Home > Back-end >  ASP.Net Core, the localhost cannot be found on Macbook
ASP.Net Core, the localhost cannot be found on Macbook

Time:11-16

I'm having a strange problem with my ASP.Net Core application. When I run in on localport from my Macbook, the project is building, but in the broser i'm seeing only this error.

This localhost page can’t be foundNo web page was found for the web address: https://localhost:5001/ HTTP ERROR 404

My question is from where that problem occurs and what is the reason for it? I'll attach my github repo to check the code if you are fine. https://github.com/ddxkalin/MovieDatabase

CodePudding user response:

Summary:

Your current problem 404 caused by you comment out the route template. But if you use the route template, you will get a new problem that method not found because your Web project is .net core3.1 which uses SigninManager with package Microsoft.AspNetCore.Identity 3.1.0, while your Services project is .netstandard 2.0 which uses SigninManager withMicrosoft.AspNetCore.Identity 2.2.0. This will causes the method not found. The two different version package uses two different constructor of the SigninManager. SigninManager in Microsoft.AspNetCore.Identity 2.2.0:

public class SignInManager<TUser> where TUser : class
{ 
    public SignInManager(UserManager<TUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<TUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<TUser>> logger, IAuthenticationSchemeProvider schemes);

SigninManager in Microsoft.AspNetCore.Identity 3.1.0:

public class SignInManager<TUser> where TUser : class
{ 
    public SignInManager(UserManager<TUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<TUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<TUser>> logger, IAuthenticationSchemeProvider schemes,
                                      IUserConfirmation<TUser> confirmation);

In MovieDatabase.Web Project, you should use route template below:

app.UseEndpoints(endpoint =>
{
    endpoint.MapControllerRoute("admin", "{area:exists}/{controller=Dashboard}/{action=Index}/{id?}");
    endpoint.MapControllerRoute(name: "default", "{controller=Home}/{action=Index}/{id?}");
    endpoint.MapRazorPages();
});

In MovieDatabase.Services project, change your project file and ApplicationSignInManager.cs like below:

1.change TargetFramework to netcoreapp3.1 and add FrameworkReference of Microsoft.AspNetCore.App:

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
 
  <FrameworkReference Include="Microsoft.AspNetCore.App" />

  <PackageReference Include="RestSharp" Version="106.6.2" />
  <PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta009" PrivateAssets="all">
    <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
  </PackageReference>
</ItemGroup>

2:Change the ApplicationSignInManager.cs:

public class ApplicationSignInManager<TUser> : SignInManager<ApplicationUser>
    where TUser : ApplicationUser
{
    public ApplicationSignInManager(
        ApplicationUserManager<ApplicationUser> userManager,
        IHttpContextAccessor contextAccessor,
        IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory,
        IOptions<IdentityOptions> optionsAccessor,
        ILogger<SignInManager<ApplicationUser>> logger,
        IAuthenticationSchemeProvider schemeProvider,
        IUserConfirmation<ApplicationUser> userConfirmation       //add this...
    )
    : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemeProvider, userConfirmation)
    {
    }
  • Related