Page Code:
@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();
}
Service Code:
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;
}
}
Getting Exception:
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: No service for type 'Umbraco.Cms.Core.IPublishedContentQuery' has been registered.
System.InvalidOperationException: No service for type 'Umbraco.Cms.Core.IPublishedContentQuery' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[IPublishedContentQuery](IServiceProvider provider)
at WebApplication1.Shared.Services.ContentDeliveryService.Search(String searchTerm)+MoveNext() in D:\Clients\Kim\Softnotik.Web\Shared\Services\ContentDeliveryService.cs:line 562
at System.Collections.Generic.List1[[Umbraco.Cms.Core.Models.PublishedContent.PublishedSearchResult, Umbraco.Core, Version=10.2.0.0, Culture=neutral, PublicKeyToken=null]]..ctor(IEnumerable
1 collection)
at System.Linq.Enumerable.ToList[PublishedSearchResult](IEnumerable`1 source)
at WebApplication1.Client.Pages.AboutUs.SearchComponent.Search() in D:\Clients\Kim\Softnotik.Web\Client\Pages\AboutUs\SearchComponent.razor:line 39
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
window.Module.s.printErr @ blazor.webassembly.js:1
DevTools failed to load source map: Could not load content for chrome-extension://gpaiobkfhnonedkhhfjpmhdalgeoebfa/editor/config.js.map: System error: net::ERR_BLOCKED_BY_CLIENT
DevTools failed to load source map: Could not load content for chrome-extension://gpaiobkfhnonedkhhfjpmhdalgeoebfa/editor/content.js.map: System error: net::ERR_BLOCKED_BY_CLIENT
search:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)