在Blazor WebAssembly解决方案中,如果服务器项目的控制器未显示登录,则需要按照以下步骤进行修复:
确保服务器项目已经添加到Blazor Web Assembly解决方案中,同时在服务器项目中添加Identity包。
在服务器项目的Startup类中,确保正确配置了Identity服务。
以下是一个示例:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ServerProject.Data;
using Microsoft.EntityFrameworkCore;
using ServerProject.Models;
using Microsoft.AspNetCore.Identity;
namespace ServerProject
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity()
.AddEntityFrameworkStores();
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
}
}
async Task Login()
{
var result = await _http.PostJsonAsync("Identity/Account/Login", new LoginModel
{
UserName = _userName,
Password = _password
});
if (result.Succeeded)
{
// Do something
}
else
{
// Show error message
}
}
通过以上步骤