Access ASP.NET 5 View Component via URL

你。 提交于 2019-12-04 08:59:47

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 :

    1. Layout page would have something like @Html.Partial("JobsListPartial").
    2. 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.
    3. 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 :

    1. Create a partial view (having your ajax scripts) and render to the client.
    2. Create a controller exposing api for navigation through pages of available job positions.
    3. Call this api(returns json) from the ajax script.
    4. Use the json data to dynamically change the UI at the client.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!