【发布时间】:2021-10-19 17:08:09
【问题描述】:
我是 Blazor 的新手,所以请原谅任何术语混淆。
我创建了一个 Blazor Web Assembly 应用程序。我勾选了“ASP.NET Core Hosted”并选择了身份验证“Use Individual Accounts”。
Visual Studio 创建了一个包含 3 个项目(客户端、服务器和共享)的基本文件结构。我将脚手架的身份页面添加到服务器项目中。我运行了迁移,并且在本地一切正常。我可以注册用户、登录、注销等。
我将我的应用程序部署到 IIS。我有两个应用程序在 IIS 的不同端口上运行。我还发布了共享项目。我还确保权限设置正确。
首先我尝试访问服务器应用程序。我尝试打开它,但它给了我一个空白页。
当我访问客户端应用程序时,网站会打开,但与身份相关的功能会被破坏。该页面仅显示“正在授权”。我检查了控制台。
我收到一个奇怪的错误:
暴击:Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
未处理的异常渲染组件:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符
Microsoft.JSInterop.JSException:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符
关于什么是错的任何想法?我在 IIS 中是否设置错误?
客户端应用程序的 Program.cs 如下所示:
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddHttpClient("Clients.Admin.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("Clients.Admin.ServerAPI"));
builder.Services.AddApiAuthorization();
await builder.Build().RunAsync();
}
}
服务器的 Startup.cs 如下所示:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DB")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddRoles<IdentityRole>()
.AddDefaultUI()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
var builder = services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
builder.AddDeveloperSigningCredential();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddLogging(loggingBuilder => { loggingBuilder.AddSeq(); });
services.AddControllersWithViews();
services.AddRazorPages();
services.AddDbContextFactory<CredoContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DB")));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
}
【问题讨论】:
-
我尝试访问服务器项目,但我添加了 /index.html 并加载了主页,但随后给了我这个错误:未处理的异常渲染组件:无法从 '_configuration/Project. Endpoints.Admin.Client'
-
问这个问题可能有点傻,但是您是否为您的 blazor 版本安装了服务器运行时?由于您是新手,这通常是人们忘记的一步。
标签: blazor webassembly