With the replacement of partial views in ASP.NET 5 with view components, how does one access the view components via URL?
I know you call them like...
@Component.Invoke("SomeList", 1)
...but what if you need to have like ajax paging, where you need a callback url to request the next set to be displayed in a partial view? So, a user can click "Load More" and it loads more from a 'partial view'.
You cannot access a view component from a URL directly. A view component is just a component of a view and that view could be a regular or partial view.
Based on your question, I believe you are trying to show the first page by default when the view (having the view component) is rendered? I have tried to put some scenarios here.
Example scenario:
Show a snippet on layout page which shows list of available job positions.
Usage cases:
Render the html related to a job list at the server side :
- Layout page would have something like @Html.Partial("JobsListPartial").
- This "JobsListPartial" would have something like await @Component.InvokeAsync("JobsListViewComponent", pageNumber). This partial view also sends ajax script to the client for users to navigate through the pages.
- At the client when user tries to navigate to a different page, the ajax script makes a call to a JobsController having an api like IActionResult GetJobs(int pageNumber) and this action returns a PartialViewResult by doing something like return PartialView("JobsListPartial", pageNumber).
Render all pages at the client side only :
- Create a partial view (having your ajax scripts) and render to the client.
- Create a controller exposing api for navigation through pages of available job positions.
- Call this api(returns json) from the ajax script.
- Use the json data to dynamically change the UI at the client.
来源:https://stackoverflow.com/questions/30583068/access-asp-net-5-view-component-via-url