首页
视频
资源
登录
原
.net core 3.x 中的ViewComponent使用方式
5001
人阅读
2020/4/16 15:17
总访问:
2538158
评论:
1
收藏:
0
手机
分类:
.net后台框架
># ViewComponent 基础 [TOC] <br/> ###### 创建一个.NetCore 3.x的MVC项目 。。。 ###### 实验目的 ------------ ```mermaid graph TD A[用户网页请求] -.->|IIS| B(HomeController) B --> C{*.cshtml中是否有ViewComponent} C -->|有| D[进入ViewComponent迷你控制器中] C -->E[不做任何处理] D -->|有| X[返回对应的处理页面] X -->O[结束] E -->O[结束] style A fill:#3498db,stroke:#2980b9,stroke-width:4px,fill-opacity:0.5 style B fill:#ccf,stroke:#9980FA,stroke-width:4px,stroke-dasharray: 10,5 style C fill:#7bed9f,stroke:#2ed573,stroke-width:4px,stroke-dasharray: 10,5 style D fill:#ccf,stroke:#9980FA,stroke-width:4px,stroke-dasharray: 10,5 style X fill:#ED4C67,color:#fff,stroke:#B53471,stroke-width:4px,stroke-dasharray: 10,5 style E fill:#000,color:#fff style O fill:#000,color:#fff ``` ###### 开始实验 ------------ >创建 <font style="color:#FFC312">**ViewComponents**</font> 创建 <font style="color:#2ed573">**CounterViewComponent.cs**</font> <br/> ![](https://img.tnblog.net/arcimg/hb/814ae00946ff498994cde381d00342e1.png) <br/> ><font style="color:#2ed573;">**CounterViewComponent.cs**</font>内容如下 <br/> ```csharp public class CounterViewComponent:ViewComponent { public static int i = 0; private readonly ILogger<HomeController> _logger; public CounterViewComponent(ILogger<HomeController> logger) { _logger = logger; } public async Task<IViewComponentResult> InvokeAsync() { var controller = RouteData.Values["controller"].ToString(); var action = RouteData.Values["action"].ToString(); if (!string.IsNullOrWhiteSpace(controller) && !string.IsNullOrWhiteSpace(action)) { var pageId = $"{controller}-{action}"; i = i++; return View("Default", pageId +": "+ i); } throw new Exception("Cannot get pageId"); } } ``` <br/> >在 <font style="color:#FFC312">**Views**</font>文件夹创建下列对应文档与路径 <br/> ![](https://img.tnblog.net/arcimg/hb/5063492443f24168a8e27e55021f6e4f.png) <br/> >在**HomeController**中的**Index**视图上添加如下代码 在这之前我把layer给去掉了 <br/> ```html <h1>@await Component.InvokeAsync("Counter")</h1> ``` ###### 实验结果 ------------ ![结果](https://img.tnblog.net/arcimg/hb/d46639404907403c88b2d169559ccd80.png "结果")
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
166篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术