通过URL访问ASP.NET 5 View组件 [英] Access ASP.NET 5 View Component via URL

查看:89
本文介绍了通过URL访问ASP.NET 5 View组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用视图组件替换ASP.NET 5中的部分视图,一个人如何通过URL访问视图组件?

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)

...但是,如果您需要像ajax分页一样,需要回调URL来请求下一组显示在局部视图中,该怎么办?因此,用户可以单击加载更多",然后从局部视图"中加载更多.

...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'.

推荐答案

您不能直接从URL访问视图组件.视图组件只是视图的组成部分,该视图可以是常规视图或部分视图.

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.

使用案例:

  • 在服务器端呈现与作业列表相关的html:

  • Render the html related to a job list at the server side :

  1. 布局页面的内容类似于 @ Html.Partial("JobsListPartial").
  2. 此"JobsListPartial"将具有类似 await @ Component.InvokeAsync("JobsListViewComponent",pageNumber)的内容.此局部视图还将ajax脚本发送给客户端,以供用户浏览页面.
  3. 在客户端上,当用户尝试导航到其他页面时,ajax脚本会调用具有 IActionResult GetJobs(int pageNumber)之类的api的 JobsController 此操作通过执行 return PartialView("JobsListPartial",pageNumber)之类的操作来返回 PartialViewResult .
  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. 创建局部视图(具有ajax脚本)并呈现给客户端.
    2. 创建一个公开api的控制器,以浏览可用职位页面.
    3. 从ajax脚本调用此api(返回json).
    4. 使用json数据动态更改客户端上的UI.

  • 这篇关于通过URL访问ASP.NET 5 View组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆