通过本文主要向大家介绍了精通asp.net web api,web程序设计asp.net,vs2010 asp.net web,简述asp.net的web窗体,asp.net等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
In this section, we'll add a Web API controller that supports CRUD (create, read, update, and delete) operations on products. The controller will use Entity Framework to communicate with the database layer. Only administrators will be able to use this controller. Customers will access the products through another controller.
在本小节中,我们要添加一个对产品支持CRUD(创建、读取、更新和删除)操作的Web API控制器。该控制器将使用实体框架与数据库层进行通信。只有管理员才能够使用这个控制器。客户端将通过另一个控制器访问产品。
In Solution Explorer, right-click the Controllers folder. Select Add and then Controller.
在“解决方案资源管理器”中右击Controllers文件夹,选择“添加”,然后选“控制器”(见图2-16)。
图2-16. 添加控制器
In the Add Controller dialog, name the controller AdminController. Under Template, select "API controller with read/write actions, using Entity Framework". Under Model class, select "Product (ProductStore.Models)". Under Data Context, select "<New Data Context>".
在“添加控制器”对话框中,将此控制器命名为AdminController。在“模板”下选择“带有读/写动作的API控制器(用实体框架)”。在“模型类”下选择“Product (ProductStore.Models)”。在“数据上下文”下选择“<新数据上下文>”(见图2-17)。
图2-17. 添加控制器对话框中的设置
If the Model class drop-down does not show any model classes, make sure you compiled the project. Entity Framework uses reflection, so it needs the compiled assembly.
如果“模型类”下拉列表未显示任何模型类,请确保已编译了此项目。实体框架使用反射,因此它需要已编译的程序集。
Selecting "<New Data Context>" will open the New Data Context dialog. Name the data context ProductStore.Models.OrdersContext.
选择“<新数据上下文>”会打开“新数据上下文”对话框。将该数据上下文命名为ProductStore.Models.OrdersContext(见图2-18)。
图2-18. 命名“新数据上下文”
Click OK to dismiss the New Data Context dialog. In the Add Controller dialog, click Add.
点击“OK”退出这个“新数据上下文”对话框。在“添加控制器”对话框中点击“添加”。
Here's what got added to the project:
以下是添加到项目的内容:
A class named OrdersContext that derives from DbContext. This class provides the glue between the POCO models and the database.
一个名称为的OrdersContext类,它派生于DbContext。这个类提供了POCO模型与数据库之间的粘合。
A Web API controller named AdminController. This controller supports CRUD operations on Product instances. It uses the OrdersContext class to communicate with Entity Framework.
一个名称为AdminController的Web API控制器。这个控制器支持对Product实例的CRUD操作。它使用OrdersContext类与实体框架进行通信。
A new database connection string in the Web.config file.
Web.config文件中的一个新的数据库连接字符串。
上述新添加项见图2-19。
图2-19. 新添加到项目的内容
Open the OrdersContext.cs file. Notice that the constructor specifies the name of the database connection string. This name refers to the connection string that was added to Web.config.
打开OrdersContext.cs文件。注意,其构造器指明了数据库连接字符串的名称。该名称是指被添加到Web.config的连接字符串。
By inheriting from the DropCreat
在本小节中,我们要添加一个对产品支持CRUD(创建、读取、更新和删除)操作的Web API控制器。该控制器将使用实体框架与数据库层进行通信。只有管理员才能够使用这个控制器。客户端将通过另一个控制器访问产品。
In Solution Explorer, right-click the Controllers folder. Select Add and then Controller.
在“解决方案资源管理器”中右击Controllers文件夹,选择“添加”,然后选“控制器”(见图2-16)。

图2-16. 添加控制器
In the Add Controller dialog, name the controller AdminController. Under Template, select "API controller with read/write actions, using Entity Framework". Under Model class, select "Product (ProductStore.Models)". Under Data Context, select "<New Data Context>".
在“添加控制器”对话框中,将此控制器命名为AdminController。在“模板”下选择“带有读/写动作的API控制器(用实体框架)”。在“模型类”下选择“Product (ProductStore.Models)”。在“数据上下文”下选择“<新数据上下文>”(见图2-17)。

图2-17. 添加控制器对话框中的设置
If the Model class drop-down does not show any model classes, make sure you compiled the project. Entity Framework uses reflection, so it needs the compiled assembly.
如果“模型类”下拉列表未显示任何模型类,请确保已编译了此项目。实体框架使用反射,因此它需要已编译的程序集。
Selecting "<New Data Context>" will open the New Data Context dialog. Name the data context ProductStore.Models.OrdersContext.
选择“<新数据上下文>”会打开“新数据上下文”对话框。将该数据上下文命名为ProductStore.Models.OrdersContext(见图2-18)。

图2-18. 命名“新数据上下文”
Click OK to dismiss the New Data Context dialog. In the Add Controller dialog, click Add.
点击“OK”退出这个“新数据上下文”对话框。在“添加控制器”对话框中点击“添加”。
Here's what got added to the project:
以下是添加到项目的内容:
A class named OrdersContext that derives from DbContext. This class provides the glue between the POCO models and the database.
一个名称为的OrdersContext类,它派生于DbContext。这个类提供了POCO模型与数据库之间的粘合。
A Web API controller named AdminController. This controller supports CRUD operations on Product instances. It uses the OrdersContext class to communicate with Entity Framework.
一个名称为AdminController的Web API控制器。这个控制器支持对Product实例的CRUD操作。它使用OrdersContext类与实体框架进行通信。
A new database connection string in the Web.config file.
Web.config文件中的一个新的数据库连接字符串。
上述新添加项见图2-19。

图2-19. 新添加到项目的内容
Open the OrdersContext.cs file. Notice that the constructor specifies the name of the database connection string. This name refers to the connection string that was added to Web.config.
打开OrdersContext.cs文件。注意,其构造器指明了数据库连接字符串的名称。该名称是指被添加到Web.config的连接字符串。
By inheriting from the DropCreat
您可能想查找下面的文章:
- ASP.NET core Web中使用appsettings.json配置文件的方法
- 详解ASP.NET WEB API 之属性路由
- ASP.NET webUploader上传大视频文件相关web.config配置
- asp.net web页面自定义分页控件使用详解
- ASP.NET Web.config配置文件详解
- ASP.NET web.config 配置节点详解
- ASP.NET Web Api 2实现多文件打包并下载文件的实例
- ASP.NET web.config中 数据库连接字符串加密解密
- 在ASP.NET 2.0中操作数据之五十三:在Data Web控件显示二进制数据
- asp.net基于Web Service实现远程上传图片的方法