ASP.NET Core MVC

MVC概述

ASP.NET Core MVC 是使用模型-视图-控制器(Model-View-Controller)设计模式构建网页应用与 API 的丰富的框架。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

asp.net core 中安装mvc

  1. services.AddControllersWithViews();//添加mvc控制器和视图服务
  2. endpoints.MapControllerRoute(//终结点默认路由

在这里插入图片描述

依赖注入

在这里插入图片描述
配置服务将对象注入到容器
在这里插入图片描述

在这里插入图片描述控制器构造时通过依赖注入从容器获取对象
在这里插入图片描述

模型

MVC 应用中的模型代表了应用的状态和业务逻辑或其可以展现的一些操作。业务逻辑应该封装在模型,连同应用持久化状态实现逻辑。强类型视图一般使用特别设计的视图模型(ViewModel)类型,它包含了视图显示需要的数据;控制器将创建并从模型填充这些视图模型。

控制器

控制器是承载用户交互、模型运转、并最终选择视图进行渲染的组件。在 MVC 应用中,视图只显示信息;控制器处理并对用户输入和交互做出响应。在 MVC 模式,控制器是最初的入口,负责选择同哪一个模型类型协作和选择哪一个视图用来呈现
在这里插入图片描述

视图

视图负责在用户界面呈现内容。它们使用 Razor 视图引擎在 HTML 标记中嵌入 .NET 代码。视图中应仅包含少量的逻辑,而这些逻辑应该是与呈现内容相关的。如果你发现需要在视图文件中完成大量的逻辑任务,以便从复杂的模型展示数据,请考虑使用视图组件视图模型、或视图模板来简化视图。

https://localhost:44369/Home/Details 默认路径下没有发现视图
在这里插入图片描述

在这里插入图片描述

视图发现

在这里插入图片描述
建议使用绝对路径
在这里插入图片描述
在这里插入图片描述

从控制器传递数据到视图

ViewData和ViewBag

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
强类型视图
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

视图模型

模型对象无法满足视图所需的所有数据时,就需要使用视图模型ViewModels了。DTO是数据传输对象,用来组合对象数据为视图提供唯一强类型模型对象,专门服务视图的模型即ViewModels。

视图模型Dto
在这里插入图片描述控制器
在这里插入图片描述
视图
在这里插入图片描述

布局页面_Layout.cshtml

简单的视图布局结构在这里插入图片描述
在这里插入图片描述
添加布局页面_Layout.cshtml
在这里插入图片描述
使用视图布局页 Layout = “~/Views/Shared/_Layout.cshtml”;
在这里插入图片描述

在这里插入图片描述

布局页面Sections

在这里插入图片描述布局页中的Section设置,默认不使用该Section Scripts节点
在这里插入图片描述

视图页使用了布局页中Section Scripts节点
在这里插入图片描述
视图页没有使用了布局页中Section Scripts节点
在这里插入图片描述

视图开始_ViewStart.cshtml

添加视图开始,_ViewStart.cshtml 通常直接放置在 /Pages(或 /Views)文件夹中。 指定所有视图都将使用 _Layout.cshtml 布局,不需要每个视图文件添加引用 Layout = “~/Views/Shared/_Layout.cshtml”;

指定的布局可以使用完整路径(例如 /Pages/Shared/_Layout.cshtml 或 /Views/Shared/_Layout.cshtml)或部分名称(示例:_Layout)。 如果提供了部分名称, Razor 视图引擎将使用其标准发现进程搜索布局文件。 首先搜索处理程序方法(或控制器)所在的文件夹,然后搜索 Shared 文件夹。

在这里插入图片描述在这里插入图片描述

视图加载布局页加载顺序优先从视图文件直接指定布局页,再依次从最靠近视图文件的文件夹中加载_ViewStart.cshtml文件来指定布局页。

视图导入_ViewImports.cshtml

视图和页面可以使用 Razor 指令导入命名空间和使用依赖关系注入。 由多个视图共享的指令可以在通用 _ViewImports.cshtml 文件中进行指定。
在这里插入图片描述
如果在文件层次结构中找到多个 _ViewImports.cshtml 文件,指令的合并行为是:

  • @addTagHelper``@removeTagHelper:按顺序全部运行
  • @tagHelperPrefix:最接近视图的文件会替代任何其他文件
  • @model:最接近视图的文件会替代任何其他文件
  • @inherits:最接近视图的文件会替代任何其他文件
  • @using:全部包括在内;忽略重复项
  • @inject:针对每个属性,最接近视图的属性会替代具有相同属性名的任何其他属性

路由

路由的主要作用是映射URL,而不需要关注服务器的物理文件结构,提高安全性,同时规范了URL请求,有利于搜索引擎优化。在Asp.NetCore中,注册路由方式有两种:

  • 模板路由注册:适合应用于MVC页面项目,相对于来说,使用模板的形式更加方便,约定大于配置,统一URL;
  • 特性路由注册(RouteAttribute):适合应用于API项目,针对于不同业务路由会进行定制,特性标注显得更加便捷;

参考:
ASP.NET Core 中路由到控制器操作
跟我一起学.NetCore之路由的最佳实现

包管理工具

LibMan
在这里插入图片描述添加客户端库
在这里插入图片描述选择库添加
在这里插入图片描述
生成库
在这里插入图片描述
管理库
在这里插入图片描述
在这里插入图片描述

标记帮助程序TagHelper

在这里插入图片描述

内置 ASP.NET Core 标记帮助程序

  • 定位点标记帮助程序
  • 缓存标记帮助程序
  • 组件标记帮助程序
  • 分布式缓存帮助程序
  • 环境标记帮助程序
  • 表单标记帮助程序
  • 窗体操作标记帮助程序
  • 图像标记帮助程序
  • 输入标记帮助程序
  • 标签标记帮助程序
  • 链接标记帮助程序
  • 部分标记帮助程序
  • 脚本标记帮助程序
  • 选择标记帮助程序
  • 文本区标记帮助程序
  • 验证消息标记帮助程序
  • 验证摘要标记帮助程序

ASP.NET Core 中的标记帮助程序

模型绑定

ASP.NET Core 中的模型绑定
在这里插入图片描述
在这里插入图片描述

添加验证

ASP.NET Core MVC 和页面中的模型验证
在模型属性上添加验证属性
在这里插入图片描述ModelState.IsValid 验证判断
在这里插入图片描述

显示验证提示
在这里插入图片描述

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Learning ASP.NET Core MVC Programming" English | ISBN: 1786463830 | 2016 | EPUB | 342 pages | 17 MB Key Features Get a first-principles coverage of ASP.NET MVC and the latest release, Core This book is uniquely designed for developers who are looking to transition their skills into the .NET development field The standalone chapter structure leaves you free to explore ASP.NET MVC to immediately solve your pain points Book Description ASP.NET Core MVC helps you build robust web applications using the Model-View-Controller design. This guide will help you in building applications which can be deployed on non-windows platforms such as Linux. In today's age, it is crucial that you possess the ability to separate the programming and business logic, and this is exactly what ASP.NET Core MVC application will help you achieve. This version comes with a number of improvements that enable fast, TDD-friendly development to create sophisticated applications. You would also learn the fundamentals of Entity framework and on how to use the same in ASP.NET Core web applications. The book presents the fundamentals and philosophies of ASP.NET Core. Starting with an overview of the MVC pattern, we quickly dive into the aspects that you need to know to get started with ASP.NET. You will learn about the core architecture of model, view, and control. Integrating your application with Bootstrap, validating user input, interacting with databases, and deploying your application are some of the things that you will be able to execute with this fast-paced guide. The end of the book will test your knowledge as you build a fully working sample application using the skills you've learned throughout the book. What you will learn Get to know the concepts of ASP.NET MVC and build a new static web page using HTML, CSS, and jQuery Set up a development environment and run a sample application using the template Create a Controller with action methods Build a view using several features of the Razor View engine Construct a Model for ASP.NET Core MVC application Devise a custom mechanism to provide maximum flexibility to your application through routing Validate the user input on the client side using jQuery Enhance your applications using Bootstrap Explore new configuration and deployment scenarios—step by step guide to deploying ASP.NET Core web application in Linux
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值