【发布时间】:2019-05-20 08:54:44
【问题描述】:
参考这篇VisualStudioMagazine 文章,我正在尝试将代码放在单独的文件中而不是剃刀视图中。
我试过了:
@page "/Item"
@using WebApplication1.Shared
@using WebApplication1.Client.Services;
@inject HttpClient Http
@inherits ItemComponent
@if (ItemList != null)
{
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Metal</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
@foreach (var item in ItemList)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Category</td>
<td>@item.Metal</td>
<td>@item.Price</td>
<td>@item.Quantity</td>
</tr>
}
</tbody>
</table>
}
@functions{
public ItemModel[] ItemList;
ItemComponent IC = new ItemComponent();
protected override async Task OnInitAsync()
{
ItemList = IC.GetItems().Result;
//ItemList = await Http.GetJsonAsync<ItemModel[]>("api/Item/GetItems");
StateHasChanged();
}
}
还有ItemComponent:
using System.Threading.Tasks;
using WebApplication1.Shared;
using System.Net.Http;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Blazor;
namespace WebApplication1.Client.Services
{
public class ItemComponent
{
public async Task<ItemModel[]> GetItems()
{
ItemModel[] ItemList;
HttpClient Http = new HttpClient();
ItemList = await Http.GetJsonAsync<ItemModel[]>("api/Item/GetItems");
return ItemList;
}
}
}
但它不起作用,它表明:
严重性代码描述项目文件行抑制状态 错误 CS0115 'Item.BuildRenderTree(RenderTreeBuilder)': 找不到合适的方法来覆盖 WebApplication1.Client D:\Other\blazor\WebApplication1.Client\obj\Debug\netstandard2.0\RazorDeclaration\Pages\ItemModule\Item.razor.g .cs 30 活跃
同样根据教程页面不能继承BlazorComponent到ItemComponent,因为它没有引用。
有没有办法将大部分代码从 Blazor 视图中分离到单独的代码文件中?
更新 1
按照 Chris Answer 进行更改后,显示异常
System.Net.Http.HttpRequestException: 由于目标机器主动拒绝,无法建立连接。 ---> System.Net.Sockets.SocketException:无法建立连接 因为目标机器主动拒绝了。在 System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32 端口, CancellationToken cancelToken) --- 内部异常结束 堆栈跟踪 --- 在 System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32 端口, CancellationToken 取消令牌)在 System.Threading.Tasks.ValueTask
1.get_Result() at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)1.get_Result() 在 System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage 请求,CancellationToken 取消令牌)在 System.Threading.Tasks.ValueTask
at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask1.get_Result() 在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancelToken)
在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 请求,CancellationToken 取消令牌)在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.Net.Http.HttpClient.GetStringAsyncCore(Task1 getTask) 在 Microsoft.AspNetCore.Builder.BlazorMonoDebugProxyAppBuilderExtensions.GetOpenedBrowserTabs(字符串 调试器主机)在 Microsoft.AspNetCore.Builder.BlazorMonoDebugProxyAppBuilderExtensions.DebugHome(HttpContext 上下文)
【问题讨论】:
标签: c# blazor .net-core-3.0 blazor-server-side asp.net-core-3.0