【发布时间】:2021-01-02 16:24:03
【问题描述】:
我正在创建一个 Blazor Web 应用程序,在这个应用程序内部我有一个 API、c# 类、接口、控制器等。当我运行该应用程序时,剃刀页面向我抛出一个与空值相关的错误。
NullReferenceException: Object reference not set to an instance of an object.
Infra.Web.Pages.Subscriptions.BuildRenderTree(RenderTreeBuilder __builder) in Subscriptions.razor, line 15
Infra.Web.Pages.Subscriptions.BuildRenderTree(RenderTreeBuilder __builder) in Subscriptions.razor
-
<th scope="col">Subscription Id</th>
<th scope="col">Subscription Name</th>
<th scope="col">Options</th>
</tr>
</thead>
<tbody>
@foreach(var sub in Subscriptions) {
<tr>
<th scope="row">@sub.SubId</th>
<td>@sub.SubscriptionId</td>
<td>@sub.SubscriptionName</td>
<td>
<a href="#" class="btn btn-primary m-1">Deploy</a>
此错误与每个“订阅”有关:
<tbody>
@foreach(var sub in Subscriptions) {
<tr>
<th scope="row">@sub.SubId</th>
<td>@sub.SubscriptionId</td>
<td>@sub.SubscriptionName</td>
<td>
<a href="#" class="btn btn-primary m-1">Deploy</a>
<a href="#" class="btn btn-danger m-1">Delete</a>
</td>
</tr>
}
</tbody>
我的 Razor 类有这个代码:
using Microsoft.AspNetCore.Components;
using Infra.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Infra.Web.Services;
namespace Infra.Web.Pages
{
public class SubscriptionsBase : ComponentBase
{
[Inject]
public ISubscriptionService SubscriptionService { get; set; }
public IEnumerable<Subscription> Subscriptions { get; set; }
protected override async Task OnInitializedAsync()
{
Subscriptions = (await SubscriptionService.GetSubscriptions()).ToList();
}
}
}
我定义了一个接口。
using Infra.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Infra.Web.Services
{
public interface ISubscriptionService
{
Task<IEnumerable<Subscription>> GetSubscriptions();
Task<Subscription> GetSubscription(int id);
}
}
还有一个班级
using Infra.Models;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Infra.Web.Services
{
public class SubscriptionService : ISubscriptionService
{
private readonly HttpClient httpClient;
public SubscriptionService(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public async Task<Subscription> GetSubscription(int id)
{
return await httpClient.GetJsonAsync<Subscription>($"api/subscriptions/{id}");
}
public async Task<IEnumerable<Subscription>> GetSubscriptions()
{
return await httpClient.GetJsonAsync<Subscription[]>("api/subscriptions");
}
}
}
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Infra.Web.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Infra.Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddHttpClient<ISubscriptionService, SubscriptionService>(client =>
{
client.BaseAddress = new Uri("https://localhost:44322/");
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
API 似乎在工作,因为我可以执行所有方法,GET、PUT、POST、DELETE,但是当我运行应用程序时,我收到了上面提到的错误。
我已经查看了代码,但我遗漏了一些东西,我没有报告任何错误或警告。 感谢所有帮助。
【问题讨论】:
-
GetJsonAsync
() 是来自库的扩展方法还是您自己构建的?因为到目前为止,至少在 .NET Core 3.1 中它默认不属于 HttpClient。在这个 return 上设置一个断点(在 return 之前将它设置为一个变量,以便于阅读)并查看这个方法的结果 -
在行做同样的诊断过程:Subscriptions = (await SubscriptionService.GetSubscriptions()).ToList();
标签: c# .net-core razor-pages blazor-client-side