Ajax with ViewComponent Ajax with ViewComponent ajax ajax

Ajax with ViewComponent


Your solution is correct. From the docs:

[ViewComponents are] not reachable directly as an HTTP endpoint, they're invoked from your code (usually in a view). A view component never handles a request.

While view components don't define endpoints like controllers, you can easily implement a controller action that returns the content of a ViewComponentResult.

So as you suggested, a lightweight controller can act as an AJAX proxy to our ViewComponent.

public class MyViewComponentController : Controller {    [HttpGet]    public IActionResult Get(int id)     {        return ViewComponent("MyViewComponent", new { id });    }}