?虽然说学习新的开发框架是一项巨大的投资,但是作为一个开发人员,不断学习新的技术并快速上手是我们应该掌握的技能,甚至是一个.NET Framework开发人员,学习.NET Core 新框架可以更快速掌握其中的编写,构建,测试,部署和维护应用程序。
{\cHFFFFFF}{\cH00FFFF} Although learning about the new development framework is a huge investment, as a developer, learning about new technologies and fast-tracking is the skill we should have, and even as a.NET Framework developer, learning.NET Core, size_16, text_QDuxQ1RP5.com/images/blog/202104/24/75a10cc8e278d592.png?x-issue=image/watermark, size_16, text_QDuxQ1RP5a56i, color_FF30, se,x_10, y_shad_20, ty_ZuZ3poZvdgk=m_rexue"""; {mt5a) = km3"
? ?您现有的.NET Framework应用程序可以在其他操作系统上工作。对于希望扩大类库的受众平台,或者希望在分布式应用程序的其他领域使用相同代码的开发人员来说,这是非常好的选择。
♪ Your current.NET Framewerk application can work on other operating systems. This is a very good option for developers who want to expand the library’s audience platform or who want to use the same code in other areas of distributed applications.
? .NET Core是为了重新启动某些Framework组件而为其他人提供跨平台工作的机会。由于.NET Framework主要以托管(C#)代码为基础构建,因此这些部分不需要更改代码即可移至.NET Core。依赖于Windows特定组件的库必须被移除或重构以使用跨平台替代方案。这同样适用于您的应用程序。
.NET Core provides other people with the opportunity to work across platforms to restart some of the Framework components. Since.NET Framework is mainly built on hosting (C#) codes, these parts can be moved to.NET Core without the need to change the code. A library that relies on selected Windows components must be removed or reconfigured to use cross-platform alternatives. This applies equally to your application.
? 选择学习.NET Core,是因为有强大的多功能性:
♪ The choice of learning.NET Core is because of the powerful multifunctionality:
加上微软社区的大量工作,使.Net Core成为市场上具有竞争力的框架,帮助开发人员快速开发具有最佳性能和可扩展性的强大应用程序。
Together with the extensive work of the Microsoft community,.Net Core has become a competitive framework in the market, helping developers to quickly develop powerful applications with optimal performance and scalability.
?在本章,我们将介绍生成 ASP.NET Core Razor Pages Web 应用的基础知识,基于ASP.NET Core应用程序中的Razor视图引擎,快速的搭建一个博主的管理系统应用,并且在后续中,我们可以基于这个模板进行快速开发迭代。
♪ In this chapter, we will introduce the basic knowledge that generates the ASP.NET Core Razor Pages Web application, based on the Razor view engine in the ASP.NET Core application, to quickly build a webmaster's management system application, and in the follow-up, we can quickly develop an overlap based on this template.
工具本文基于以下开发工具和程序包:
This paper is based on the following development tools and programme packages:
1.VS2019
包含 ASP.NET Web开发功能模块
Include ASP.NET Web development functionality module
2. .NET Core SDK 3.0版本
2..NET Core SDK version 3.0
开始1. 下一步:
Next steps:
?
2.将项目命名为“BlogRazor” 。
Name the project as "BlogRazor".
3.选择ASP.NET Core3.0 , 然后依次选择“Web 应用程序”和“创建”
Select ASP.NET Core3.0 and then choose "Web Application" and "Crew" in turn.
4.项目初建成功:
4. Project start-up success:
?
?
1.添加数据模型
1. Adding data models
?创建新文件夹Models,在此文件夹下右键 选择“添加” > “类” ,创建BloggerModel类
? Create new folder Models, right below this folder Select Add & gt; Classes to create BloggerModel Class
using?System;using?System.Collections.Generic;using?System.ComponentModel.DataAnnotations;using?System.Linq;using?System.Threading.Tasks;namespace?BlogRazor.Models {public?class?BloggerModel {public?int?ID?{?get;?set;?}public?string?Name?{?get;?set;?}public?string?Sex?{?get;?set;?}public?string?Sign?{?get;?set;?} [DataType(DataType.Date)]public?DateTime?RegistDate?{?get;?set;?} } }
2. 添加模型基架
2. Add model frames
创建“Pages/Blogger”文件夹——在Blogger文件夹右键 选择添加 新搭建基架的项目
Create the "Pages/Blogger" folder -- select the right button for the Blogger folder to add a new building frame
?
?3. 在“添加基架”对话框中,选择“使用实体框架生成 Razor Pages (CRUD)”>“添加” 。
?3. In the Adding Frame dialogue box, select "Use the Physical Frame to Generate Razor Pages (CRUD)"> Add.
4 .完成“使用实体框架(CRUD)添加 Razor Pages”对话框 :
Completion of the "CRUD Add Razor Pages" dialogue:
- 在“模型类”下拉列表中,选择“BloggerModel (BlogRazor.Models) 。
- 在“数据上下文类”行中,选择 +(加号)并将生成的名称从 BlogRazor.Models .BlogRazorContext 更改为 BlogRazor.Data .Data.Context 。?它创建具有正确命名空间的数据库上下文类。
- 选择“添加” 。
4. 开始创建文件,在搭建基架时,会创建并更新以下文件
4. Start creating files, creating and updating the following files during the build-up of the base
- Pages/Blogger:“创建”、“删除”、“详细信息”、“编辑”和“索引”。
- Data/BlogRazorContext.cs
?
5 . 数据初始迁移
5. Initial migration of data
通过程序包管理器控制台 (PMC) :
Pass package manager (PMC):
Add-Migration?InitialCreate Update-Database
?
?
?
?
说明?这里已Blogger/Index.cshtml Razor页面为例
This is the example of Blogger/Index.cshtml Razor page.
@page @model?BlogRazor.Pages.Blogger.IndexModel @{ ViewData["Title"]?=?"Index"; }<table?class="table">@foreach?(var?item?in?Model.BloggerModel)?{}
public?class?IndexModel?:?PageModel {private?readonly?BlogRazor.Models.Data.Context?_context;public?IndexModel(BlogRazor.Models.Data.Context?context) { _context?=?context; }public?IListBloggerModel?{?get;set;?}public?async?Task?OnGetAsync() { BloggerModel?=?await?_context.BloggerModel.ToListAsync(); } }
Razor 可以从 HTML 转换为 C# 或 Razor 特定标记。?当?@?符号后跟?Razor 保留关键字时,它会转换为 Razor 特定标记,否则会转换为 C#。
Razor can be converted from HTML to C# or to Razor's specific mark. When the symbol is followed by Razor retains a key , it will be converted to Razor's specific mark, otherwise it will be converted to C#.
@page?Razor 指令将文件转换为一个 MVC 操作,这意味着它可以处理请求。?@page?必须是页面上的第一个 Razor 指令。?@page?是转换到 Razor 特定标记的一个示例。
@page? Razor command converts the file to a MVC operation, which means that it can handle the request. @page? Must be the first Razor command on the page. @page? is an example of conversion to a specific Razor tag.
@model?指令指定传递给 Razor 页面的模型类型。?在前面的示例中,@model?行使?PageModel?派生的类可用于 Razor 页面。
@model? The command specifies the model type to be passed on to the Razor page. In the preceding example, @model?exercise?PageModel? is a given class that can be used on the Razor page.
@model?BlogRazor.Pages.Blogger.IndexModel
?
@{ ViewData["Title"]?=?"Index"; }
上面的代码就是 Razor 代码转换为 C# 的一个示例。 大括号“{ }” 字符括住 C# 代码块。
The code above is an example of the conversion of the Razor code to C#. The character "{}" in parentheses contains the C# code block.
PageModel 基类具有 ViewData 字典属性,可用于添加要传递到某个视图的数据。 可以使用键/值模式将对象添加到 ViewData 字典。 在上面的示例中,“Title”属性被添加到 ViewData 字典中。 “Title”属性的在 Pages/_Layout.cshtml 文件中使用。
The PageModel base class has the ViewData dictionary properties that can be used to add data to a view. The key/value mode can be used to add objects to the ViewData dictionary. In the above example, the "Title" properties are added to the ViewData dictionary. The "Title" properties are used in Pages/_Layout.cshtml files.
"Title"?属性用于 Pages/Shared/_Layout.cshtml 文件 。?以下标记显示 _Layout.cshtml 文件的前几行 。
The "Title" attribute is used in Pages/ Shared/_Layout.cshtml file. The tag below shows the first lines of the _Layout.cshtml file.
<html?lang="en"><meta?charset="utf-8"?/><meta?name="viewport"?content="width=device-width,?initial-scale=1.0"?/>@ViewData["Title"]?-?BlogRazor<link?rel="stylesheet"?href="https://blog.51cto.com/15162069/~/lib/bootstrap/dist/css/bootstrap.min.css"?/><link?rel="stylesheet"?href="https://blog.51cto.com/15162069/~/css/site.css"?/>
1.更改 Pages/Shared/_Layout.cshtml 文件中的元素以显示 博主管理系统 而不是 BlogRazor。
1. Change the elements in the Pages/Shared/_Layout.cshtml file to show the Boss Management System instead of BlogRazor.
<html?lang="en"><meta?charset="utf-8"?/><meta?name="viewport"?content="width=device-width,?initial-scale=1.0"?/>@ViewData["Title"]?-?博主管理系统
2.?asp-page="/Blogger/Index"?标记帮助程序属性和值可以创建指向?/Blogger/Index?Razor 页面的链接。?asp-area?属性值为空,因此在链接中未使用区域。
2.?asp-page="/Blogger/Index"? Marker help program properties and values create links to the pointer?/Blogger/Index?Razar page.?asp-area? attribute values are empty, so no areas are used in the link.
?LocalDB 是轻型版的 SQL Server Express 数据库引擎,以程序开发为目标。
? LocalDB is a light version of the SQL Server Express database engine, targeting program development.
?
?通过数据库管理器,我们可以查看创建的数据库和数据库表
? Through the database manager, we can view the created database and database table
?
?
默认情况下,LocalDB 数据库在?C:\Users\?目录下创建?*.mdf?文件。(右键查看数据库 属性,根据连接参数下的数据文件找到对应的文件目录)
By default, the LocalDB database is created under?C:\Users\?dir?*.mdf? file. (Right key to view database properties, find the corresponding file directory based on data files under connection parameters)
总结?1.参考文档?ASP.NET Core Web应用程序开发
?1. Reference document 2.Razor易于使用,易于学习,并且可以在Visual Studio等工具中使用,快速搭建基本的应用程序。 Razor is easy to use, easy to learn and can be used in tools such as Visual Studio to quickly build basic applications. ?
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论