<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:42:16
        文檔

        ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享

        ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享: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
        推薦度:
        導(dǎo)讀ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享: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

        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.
        在本小節(jié)中,我們要添加一個(gè)對(duì)產(chǎn)品支持CRUD(創(chuàng)建、讀取、更新和刪除)操作的Web API控制器。該控制器將使用實(shí)體框架與數(shù)據(jù)庫(kù)層進(jìn)行通信。只有管理員才能夠使用這個(gè)控制器。客戶(hù)端將通過(guò)另一個(gè)控制器訪(fǎng)問(wèn)產(chǎn)品。
        In Solution Explorer, right-click the Controllers folder. Select Add and then Controller.
        在“解決方案資源管理器”中右擊Controllers文件夾,選擇“添加”,然后選“控制器”(見(jiàn)圖2-16)。
        WebAPI2-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>".
        在“添加控制器”對(duì)話(huà)框中,將此控制器命名為AdminController。在“模板”下選擇“帶有讀/寫(xiě)動(dòng)作的API控制器(用實(shí)體框架)”。在“模型類(lèi)”下選擇“Product (ProductStore.Models)”。在“數(shù)據(jù)上下文”下選擇“<新數(shù)據(jù)上下文>”(見(jiàn)圖2-17)。
        WebAPI2-17 
        圖2-17. 添加控制器對(duì)話(huà)框中的設(shè)置
        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.
        如果“模型類(lèi)”下拉列表未顯示任何模型類(lèi),請(qǐng)確保已編譯了此項(xiàng)目。實(shí)體框架使用反射,因此它需要已編譯的程序集。
        Selecting "<New Data Context>" will open the New Data Context dialog. Name the data context ProductStore.Models.OrdersContext.
        選擇“<新數(shù)據(jù)上下文>”會(huì)打開(kāi)“新數(shù)據(jù)上下文”對(duì)話(huà)框。將該數(shù)據(jù)上下文命名為ProductStore.Models.OrdersContext(見(jiàn)圖2-18)。
        WebAPI2-18 
        圖2-18. 命名“新數(shù)據(jù)上下文”
        Click OK to dismiss the New Data Context dialog. In the Add Controller dialog, click Add.
        點(diǎn)擊“OK”退出這個(gè)“新數(shù)據(jù)上下文”對(duì)話(huà)框。在“添加控制器”對(duì)話(huà)框中點(diǎn)擊“添加”。
        Here's what got added to the project:
        以下是添加到項(xiàng)目的內(nèi)容:
        A class named OrdersContext that derives from DbContext. This class provides the glue between the POCO models and the database.
        一個(gè)名稱(chēng)為的OrdersContext類(lèi),它派生于DbContext。這個(gè)類(lèi)提供了POCO模型與數(shù)據(jù)庫(kù)之間的粘合。
        A Web API controller named AdminController. This controller supports CRUD operations on Product instances. It uses the OrdersContext class to communicate with Entity Framework.
        一個(gè)名稱(chēng)為AdminController的Web API控制器。這個(gè)控制器支持對(duì)Product實(shí)例的CRUD操作。它使用OrdersContext類(lèi)與實(shí)體框架進(jìn)行通信。
        A new database connection string in the Web.config file.
        Web.config文件中的一個(gè)新的數(shù)據(jù)庫(kù)連接字符串。
        上述新添加項(xiàng)見(jiàn)圖2-19。
        WebAPI2-19 
        圖2-19. 新添加到項(xiàng)目的內(nèi)容
        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.
        打開(kāi)OrdersContext.cs文件。注意,其構(gòu)造器指明了數(shù)據(jù)庫(kù)連接字符串的名稱(chēng)。該名稱(chēng)是指被添加到Web.config的連接字符串。
        代碼如下:

        public OrdersContext() : base("name=OrdersContext")Add the following properties to the OrdersContext class:

        將以下屬性添加到OrdersContext類(lèi):
        代碼如下:

        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }

        A DbSet represents a set of entities that can be queried. Here is the complete listing for the OrdersContext class:
        DbSet表示一組能夠被查詢(xún)的實(shí)體。以下是這個(gè)OrdersContext類(lèi)的完整清單:
        代碼如下:

        public class OrdersContext : DbContext
        {
        public OrdersContext() : base("name=OrdersContext")
        {
        }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public DbSet<Product> Products { get; set; }
        }

        The AdminController class defines five methods that implement basic CRUD functionality. Each method corresponds to a URI that the client can invoke:
        類(lèi)定義了實(shí)現(xiàn)基本的CRUD功能的五個(gè)方法。每個(gè)方法對(duì)應(yīng)于一個(gè)客戶(hù)端可以請(qǐng)求的URI(見(jiàn)表2-2):
        表2-2. AdminController中實(shí)現(xiàn)CRUD操作的五個(gè)方法
        table
        Each method calls into OrdersContext to query the database. The methods that modify the collection (PUT, POST, and DELETE) call db.SaveChanges to persist the changes to the database. Controllers are created per HTTP request and then disposed, so it is necessary to persist changes before a method returns.
        每一個(gè)方法調(diào)用都會(huì)進(jìn)入OrdersContext對(duì)數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)。對(duì)數(shù)據(jù)集進(jìn)行修改的方法(PUT、POST以及DELETE)會(huì)調(diào)用db.SaveChanges,以便把這些修改持久化回?cái)?shù)據(jù)庫(kù)。每個(gè)HTTP請(qǐng)求都會(huì)創(chuàng)建控制器(實(shí)例),然后清除它。因此,在一個(gè)方法返回之前,對(duì)修改持久化是必要的。
        Add a Database Initializer
        添加數(shù)據(jù)庫(kù)初始化器
        Entity Framework has a nice feature that lets you populate the database on startup, and automatically recreate the database whenever the models change. This feature is useful during development, because you always have some test data, even if you change the models.
        實(shí)體框架有一個(gè)很好的特性,它讓你在(應(yīng)用程序)啟動(dòng)時(shí)填充數(shù)據(jù)庫(kù),并在模型發(fā)生修改時(shí)重建數(shù)據(jù)庫(kù)。這個(gè)特性在開(kāi)發(fā)期間是有用的,因?yàn)槟憧倳?huì)有一些測(cè)試數(shù)據(jù),甚至?xí)薷哪P汀?
        In Solution Explorer, right-click the Models folder and create a new class named OrdersContextInitializer. Paste in the following implementation:
        在“解決方案資源管理器”中,右擊Models文件夾,并創(chuàng)建一個(gè)名稱(chēng)為OrdersContextInitializer的新類(lèi)。粘貼以下實(shí)現(xiàn):
        代碼如下:

        namespace ProductStore.Models
        {
        using System;
        using System.Collections.Generic;
        using System.Data.Entity;
        public class OrdersContextInitializer : DropCreateDatabaseIfModelChanges<OrdersContext>
        {
        protected override void Seed(OrdersContext context)
        {
        var products = new List<Product>()
        {
        new Product() { Name = "Tomato Soup", Price = 1.39M, ActualCost = .99M },
        new Product() { Name = "Hammer", Price = 16.99M, ActualCost = 10 },
        new Product() { Name = "Yo yo", Price = 6.99M, ActualCost = 2.05M }
        };
        products.ForEach(p => context.Products.Add(p));
        context.SaveChanges();
        var order = new Order() { Customer = "Bob" };
        var od = new List<OrderDetail>()
        {
        new OrderDetail() { Product = products[0], Quantity = 2, Order = order},
        new OrderDetail() { Product = products[1], Quantity = 4, Order = order }
        };
        context.Orders.Add(order);
        od.ForEach(o => context.OrderDetails.Add(o));
        context.SaveChanges();
        }
        }
        }

        By inheriting from the DropCreateDatabaseIfModelChanges class, we are telling Entity Framework to drop the database whenever we modify the model classes. When Entity Framework creates (or recreates) the database, it calls the Seed method to populate the tables. We use the Seed method to add some example products plus an example order.
        通過(guò)對(duì)DropCreateDatabaseIfModelChanges類(lèi)的繼承,我們是在告訴實(shí)體框架,無(wú)論何時(shí)修改了模型類(lèi),便刪除數(shù)據(jù)庫(kù)。當(dāng)實(shí)體框架創(chuàng)建(或重建)數(shù)據(jù)庫(kù)時(shí),它會(huì)調(diào)用Seed方法去填充數(shù)據(jù)庫(kù)。我們用這個(gè)Seed方法添加了一些例子產(chǎn)品和一個(gè)例子訂單。
        This feature is great for testing, but don't use the DropCreateDatabaseIfModelChanges class in production, because you could lose your data if someone changes a model class.
        這個(gè)特性對(duì)于測(cè)試是很棒的,但在產(chǎn)品(指正式運(yùn)行的應(yīng)用程序 — 譯者注)中不要使用這個(gè)DropCreateDatabaseIfModelChanges類(lèi)。因?yàn)椋绻腥诵薷牧四P皖?lèi),便會(huì)丟失數(shù)據(jù)。
        Next, open Global.asax and add the following code to the Application_Start method:
        下一步,打開(kāi)Global.asax,并將以下代碼添加到Application_Start方法中:
        代碼如下:

        System.Data.Entity.Database.SetInitializer(
        new ProductStore.Models.OrdersContextInitializer());Send a Request to the Controller

        向控制器發(fā)送請(qǐng)求
        At this point, we haven't written any client code, but you can invoke the web API using a web browser or an HTTP debugging tool such as Fiddler. In Visual Studio, press F5 to start debugging. Your web browser will open to http://localhost:portnum/, where portnum is some port number.
        此刻,我們還沒(méi)有編寫(xiě)任何客戶(hù)端代碼,但你已經(jīng)可以使用Web瀏覽器或諸如Fiddler之類(lèi)的調(diào)試工具來(lái)調(diào)用這個(gè)Web API了。在Visual Studio中按F5鍵啟動(dòng)調(diào)試。你的瀏覽器將打開(kāi)網(wǎng)址http://localhost:portnum/,這里,portnum是某個(gè)端口號(hào)。
        Send an HTTP request to "http://localhost:portnum/api/admin". The first request may be slow to complete, because Entify Entity Framework needs to create and seed the database. The response should something similar to the following:
        發(fā)送一個(gè)HTTP請(qǐng)求到“http://localhost:portnum/api/admin”。第一次請(qǐng)求可能會(huì)慢一些才能完成,因?yàn)閷?shí)體框架需要?jiǎng)?chuàng)建和種植數(shù)據(jù)庫(kù)。其響應(yīng)應(yīng)當(dāng)類(lèi)似于下面這樣:
        代碼如下:

        HTTP/1.1 200 OK
        Server: ASP.NET Development Server/10.0.0.0
        Date: Mon, 18 Jun 2012 04:30:33 GMT
        X-AspNet-Version: 4.0.30319
        Cache-Control: no-cache
        Pragma: no-cache
        Expires: -1
        Content-Type: application/json; charset=utf-8
        Content-Length: 175
        Connection: Close
        [{"Id":1,"Name":"Tomato Soup","Price":1.39,"ActualCost":0.99},{"Id":2,"Name":"Hammer",
        "Price":16.99,"ActualCost":10.00},{"Id":3,"Name":"Yo yo","Price":6.99,"ActualCost":
        2.05}]

        看完此文如果覺(jué)得有所收獲,懇請(qǐng)給個(gè)推薦

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享

        ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享: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
        推薦度:
        標(biāo)簽: 教程 管理員 API
        • 熱門(mén)焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門(mén)推薦

        專(zhuān)題
        Top
        主站蜘蛛池模板: 免费看国产曰批40分钟| 亚洲AV无码乱码国产麻豆穿越 | eeuss影院免费直达入口| 国产V亚洲V天堂A无码| 91精品视频免费| 噜噜噜亚洲色成人网站| 亚洲国产精品嫩草影院在线观看 | 久久成人国产精品免费软件| 亚洲国产成人精品无码区二本| 成人亚洲性情网站WWW在线观看 | 精品国产亚洲一区二区三区| 青青久在线视频免费观看| 免费无码午夜福利片| 日本久久久久亚洲中字幕| 亚洲国产a级视频| 国产人在线成免费视频| 一色屋成人免费精品网站| 黄色一级毛片免费看| 亚洲一区二区三区久久| 波多野结衣免费一区视频| 亚洲老熟女五十路老熟女bbw| 亚洲中文字幕无码爆乳AV| 精品免费国产一区二区三区| 在线成人精品国产区免费| 久久水蜜桃亚洲AV无码精品| 亚洲日本在线免费观看| 国产亚洲人成A在线V网站| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲视频小说图片| 亚洲中文字幕无码久久精品1 | 亚洲av无码专区国产乱码在线观看| 日韩成人免费在线| 免费h片在线观看网址最新| 国产成人无码免费网站| 亚洲日韩在线中文字幕综合| 亚洲网站视频在线观看| 91在线手机精品免费观看| 一级黄色毛片免费看| 亚洲成熟丰满熟妇高潮XXXXX| 亚洲美女自拍视频| 亚洲Av无码精品色午夜|