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)
This is not a very good question. You aren't asking anything. You don't really describe what you have tried, other than some bits of code out of context. You don't provide us with much info to help you figure it out. And that's what we want to do - we don't want to write your code for you, we want you to figure out how to do it yourself :-)
That said, have you looked at https://docs.umbraco.com/v/10.x-lts/umbraco-cms/implementation/services ? I can't see the context of your "Service code" method - is it in an actual service? In a controller? - but the docs say this:
So if your code is in a service object and doesn't have an HttpContext/UmbracoContext of sorts, it might need it via UmbracoContextFactory.
But it's pretty much a blind guess, I'm afraid. Maybe it'll get you closer to being able to search.