Umbraco 10搜索表单异常:IPublishedContentQuery服务未注册
在Umbraco 10中实现搜索功能的问题解决
需求
在Umbraco 10中实现搜索表单/搜索框,通过关键词获取相关或精确数据。
现有代码
Blazor页面组件代码
@page "/search" @using WebApplication1.Shared.Models; @using WebApplication1.Shared.Services; @inject IContentDeliveryService _contentDeliveryService; <EditForm Model="@searchModel" OnSubmit="Search"> <InputText id="SearchTerm" class="form-control" @bind-Value="searchModel.SearchTerm" /> <button type="submit" class="btn btn-success">Submit</button> </EditForm> <div> @if (publishSearchResults != null) { foreach (var item in publishSearchResults) { <ul> <li>item</li> </ul> } } </div> private SearchModel searchModel = new SearchModel(); public string searchterm; List<Umbraco.Cms.Core.Models.PublishedContent.PublishedSearchResult> publishSearchResults = new (); protected async Task Search() { var data = _contentDeliveryService.Search(searchModel.SearchTerm); publishSearchResults = data.Cast<Umbraco.Cms.Core.Models.PublishedContent.PublishedSearchResult> ().ToList(); }
服务层代码
public IEnumerable<PublishedSearchResult> Search(string searchTerm) { using var scope = _scopeFactory.CreateScope(); var myService = scope.ServiceProvider.GetRequiredService<Umbraco.Cms.Core.IPublishedContentQuery>(); foreach (var result in myService.Search(searchTerm)) { yield return result; } }
异常信息(已翻译)
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的组件渲染异常:未注册类型为'Umbraco.Cms.Core.IPublishedContentQuery'的服务。 System.InvalidOperationException: 未注册类型为'Umbraco.Cms.Core.IPublishedContentQuery'的服务。 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[IPublishedContentQuery](IServiceProvider provider) 在 WebApplication1.Shared.Services.ContentDeliveryService.Search(String searchTerm)+MoveNext() 位置 D:\Clients\Kim\Softnotik.Web\Shared\Services\ContentDeliveryService.cs:行号 562 在 System.Collections.Generic.List`1[[Umbraco.Cms.Core.Models.PublishedContent.PublishedSearchResult, Umbraco.Core, Version=10.2.0.0, Culture=neutral, PublicKeyToken=null]]..ctor(IEnumerable`1 collection) 在 System.Linq.Enumerable.ToList[PublishedSearchResult](IEnumerable`1 source) 在 WebApplication1.Client.Pages.AboutUs.SearchComponent.Search() 位置 D:\Clients\Kim\Softnotik.Web\Client\Pages\AboutUs\SearchComponent.razor:行号 39 在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) 在 Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync() 在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) 在 Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState) window.Module.s.printErr @ blazor.webassembly.js:1 DevTools无法加载源映射:无法加载chrome-extension://gpaiobkfhnonedkhhfjpmhdalgeoebfa/editor/config.js.map的内容:系统错误:net::ERR_BLOCKED_BY_CLIENT DevTools无法加载源映射:无法加载chrome-extension://gpaiobkfhnonedkhhfjpmhdalgeoebfa/editor/content.js.map的内容:系统错误:net::ERR_BLOCKED_BY_CLIENT search:1 加载资源失败:服务器响应状态码为500(内部服务器错误)
问题原因及解决方案
原因
IPublishedContentQuery是Umbraco的服务器端服务,无法直接在Blazor WASM客户端环境中通过依赖注入获取,且当前代码未在服务容器中正确注册该服务的调用逻辑。
解决方案
- 服务器端创建API端点
在Umbraco项目的Web层添加API控制器,统一处理搜索请求:
[ApiController] [Route("api/search")] public class SearchApiController : ControllerBase { private readonly IPublishedContentQuery _publishedContentQuery; public SearchApiController(IPublishedContentQuery publishedContentQuery) { _publishedContentQuery = publishedContentQuery; } [HttpGet] public IEnumerable<PublishedSearchResult> Search(string term) { return _publishedContentQuery.Search(term); } }
- 修改客户端服务逻辑
客户端服务改为通过HTTP请求调用服务器API:
public class ContentDeliveryService : IContentDeliveryService { private readonly HttpClient _httpClient; public ContentDeliveryService(HttpClient httpClient) { _httpClient = httpClient; } public async Task<IEnumerable<PublishedSearchResult>> Search(string searchTerm) { var encodedTerm = Uri.EscapeDataString(searchTerm); return await _httpClient.GetFromJsonAsync<IEnumerable<PublishedSearchResult>>($"/api/search?term={encodedTerm}"); } }
- 更新Blazor组件的Search方法
调整为异步调用逻辑:
protected async Task Search() { publishSearchResults = (await _contentDeliveryService.Search(searchModel.SearchTerm)).ToList(); }
- 注册客户端服务
在客户端Program.cs中添加服务注册:
builder.Services.AddScoped<IContentDeliveryService, ContentDeliveryService>(); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
内容的提问来源于stack exchange,提问作者Naman K.Rawat
相关产品推荐
相关产品推荐

