• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guā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)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

    ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹

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

    ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹

    ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹:Now we'll turn to the client side, and add a page that can consume data from the Admin controller. The page will allow users to create, edit, or delete products, by sending AJAX requests to the controller. 現(xiàn)在我們轉(zhuǎn)入客戶端,并添加一個能夠使用從Admin控制器而來的數(shù)據(jù)的
    推薦度:
    導(dǎo)讀ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹:Now we'll turn to the client side, and add a page that can consume data from the Admin controller. The page will allow users to create, edit, or delete products, by sending AJAX requests to the controller. 現(xiàn)在我們轉(zhuǎn)入客戶端,并添加一個能夠使用從Admin控制器而來的數(shù)據(jù)的

    Now we'll turn to the client side, and add a page that can consume data from the Admin controller. The page will allow users to create, edit, or delete products, by sending AJAX requests to the controller.
    現(xiàn)在我們轉(zhuǎn)入客戶端,并添加一個能夠使用從Admin控制器而來的數(shù)據(jù)的頁面。通過給控制器發(fā)送AJAX請求的方式,該頁面將允許用戶創(chuàng)建、編輯,或刪除產(chǎn)品。
    In Solution Explorer, expand the Controllers folder and open the file named HomeController.cs. This file contains an MVC controller. Add a method named Admin:
    在“解決方案資源管理器”中,展開Controllers文件夾,并打開名為HomeController.cs的文件。這個文件是一個MVC控制器。添加一個名稱為Admin的方法:
    代碼如下:

    public ActionResult Admin()
    {
    string apiUri= Url.HttpRouteUrl("DefaultApi", new { controller = "admin", });
    ViewBag.ApiUrl = new Uri(Request.Url, apiUri).AbsoluteUri.ToString();
    return View();
    }

    The HttpRouteUrl method creates the URI to the web API, and we store this in the view bag for later.
    HttpRouteUrl方法創(chuàng)建了發(fā)送給Web API的URI,我們隨后把它存儲在視圖包(view bag)中。
    Next, position the text cursor within the Admin action method, then right-click and select Add View. This will bring up the Add View dialog.
    下一步,把文本光標定位到Admin動作方法的內(nèi)部,然后右擊,并選擇“添加視圖”。這會帶出“添加視圖”對話框(見圖2-20)。
    WebAPI2-20 
    圖2-20. 添加視圖
    In the Add View dialog, name the view "Admin". Select the check box labeled Create a strongly-typed view. Under Model Class, select "Product (ProductStore.Models)". Leave all the other options as their default values.
    在“添加視圖”對話框中,將此視圖命名為“Admin”。選中標簽為“創(chuàng)建強類型視圖”的復(fù)選框。在“模型類”下面,選擇“Product (ProductStore.Models)”。保留所有其它選項為其默認值(如圖2-21)。
    WebAPI2-21 
    圖2-21. “添加視圖”對話框的設(shè)置
    Clicking Add adds a file named Admin.cshtml under Views/Home. Open this file and add the following HTML. This HTML defines the structure of the page, but no functionality is wired up yet.
    點擊“添加”,會把一個名稱為Admin.cshtml的文件添加到Views/Home下。打開這個文件,并添加以下HTML。這個HTML定義了頁面的結(jié)構(gòu),但尚未連接功能。
    代碼如下:

    <div class="content">
    <div class="float-left">
    <ul id="update-products">
    <li>
    <div><div class="item">Product ID</div><span></span></div>
    <div><div class="item">Name</div> <input type="text" /></div>
    <div><div class="item">Price ($)</div> <input type="text" /></div>
    <div><div class="item">Actual Cost ($)</div> <input type="text" /></div>
    <div>
    <input type="button" value="Update" />
    <input type="button" value="Delete Item" />
    </div>
    </li>
    </ul>
    </div>
    <div class="float-right">
    <h2>Add New Product</h2>
    <form id="product">
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>Contact</legend>
    @Html.EditorForModel()
    <p>
    <input type="submit" value="Save" />
    </p>
    </fieldset>
    </form>
    </div>
    </div>

    Create a Link to the Admin Page
    創(chuàng)建到Admin頁面的鏈接
    In Solution Explorer, expand the Views folder and then expand the Shared folder. Open the file named _Layout.cshtml. Locate the ul element with id = "menu", and an action link for the Admin view:
    在“解決方案資源管理器”中,展開Views文件夾,然后展開Shared文件夾。打開名稱為_Layout.cshtml的文件。定位到id = "menu"的ul元素,和一個用于Admin視圖的動作鏈接:
    代碼如下:

    <li>@Html.ActionLink("Admin", "Admin", "Home")</li>

    In the sample project, I made a few other cosmetic changes, such as replacing the string “Your logo here”. These don't affect the functionality of the application. You can download the project and compare the files.
    在這個例子項目中,我做了幾個其它裝飾性的修改,如替換了字符串“Your logo here(這是你的logo)”。這些不會影響此應(yīng)用程序的功能。你可以下載這個項目并比較此文件。
    Run the application and click the “Admin” link that appears at the top of the home page. The Admin page should look like the following:
    運行該應(yīng)用程序,并點擊出現(xiàn)在首頁頂部的這個“Admin”鏈接。Admin頁面看上去應(yīng)當(dāng)像這樣(見圖2-22):
    WebAPI2-22
    圖2-22. Admin頁面
    Right now, the page doesn't do anything. In the next section, we'll use Knockout.js to create a dynamic UI.
    此刻,這個頁面不做任何事情。在下一小節(jié)中,我們將使用Knockout.js來創(chuàng)建一個動態(tài)UI。
    Add Authorization
    添加授權(quán)
    The Admin page is currently accessible to anyone visiting the site. Let's change this to restrict permission to administrators.
    Admin此刻可以被任何訪問網(wǎng)站的人所訪問。讓我們做點修改,把許可限制到管理員。
    Start by adding an "Administrator" role and an administrator user. In Solution Explorer, expand the Filters folder and open the file named InitializeSimpleMembershipAttribute.cs. Locate the SimpleMembershipInitializer constructor. After the call to WebSecurity.InitializeDatabaseConnection, add the following code:
    先從添加“Administrator(管理員)”角色和administrator用戶開始。在“解決方案資源管理器”中,展開Filters文件夾,并打開名稱為InitializeSimpleMembershipAttribute.cs的文件,定位到SimpleMembershipInitializer構(gòu)造器。在對WebSecurity.InitializeDatabaseConnection的調(diào)用之后,添加以下代碼:
    代碼如下:

    const string adminRole = "Administrator";
    const string adminName = "Administrator";
    if (!Roles.RoleExists(adminRole))
    {
    Roles.CreateRole(adminRole);
    }
    if (!WebSecurity.UserExists(adminName))
    {
    WebSecurity.CreateUserAndAccount(adminName, "password");
    Roles.AddUserToRole(adminName, adminRole);
    }

    This is a quick-and-dirty way to add the "Administrator" role and create a user for the role.
    這是添加“Administrator”角色并為該角色創(chuàng)建用戶的一種快速而直接的方式。
    In Solution Explorer, expand the Controllers folder and open the HomeController.cs file. Add the Authorize attribute to the Admin method.
    在“解決方案資源管理器”中,展開Controllers文件夾,并打開HomeController.cs文件。把Authorize(授權(quán))注解屬性添加到Admin方法上:
    代碼如下:

    [Authorize(Roles="Administrator")]
    public ActionResult Admin()
    {
    return View();
    }Open the AdminController.cs file and add the Authorize attribute to the entire AdminController class.
    打開AdminController.cs文件,并把Authorize注解屬性添加到整個AdminController類上:
    [Authorize(Roles="Administrator")]
    public class AdminController : ApiController
    {
    // ...

    MVC and Web API both define Authorize attributes, in different namespaces. MVC uses System.Web.Mvc.AuthorizeAttribute, while Web API uses System.Web.Http.AuthorizeAttribute.
    MVC和Web API都定義了Authorize注解屬性,但位于不同的命名空間。MVC使用的是System.Web.Mvc.AuthorizeAttribute,而Web API使用System.Web.Http.AuthorizeAttribute。
    Now only administrators can view the Admin page. Also, if you send an HTTP request to the Admin controller, the request must contain an authentication cookie. If not, the server sends an HTTP 401 (Unauthorized) response. You can see this in Fiddler by sending a GET request to http://localhost:port/api/admin.
    現(xiàn)在,只有管理員才可以查看Admin頁面。而且,如果對Admin控制器發(fā)送一個HTTP請求,該請求必須包含一個認證cookie。否則,服務(wù)器會發(fā)送一個HTTP 401(未授權(quán))響應(yīng)。在Fiddler中,通過發(fā)送一個http://localhost:port/api/admin的GET請求,便會看到這種情況。

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

    文檔

    ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹

    ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹:Now we'll turn to the client side, and add a page that can consume data from the Admin controller. The page will allow users to create, edit, or delete products, by sending AJAX requests to the controller. 現(xiàn)在我們轉(zhuǎn)入客戶端,并添加一個能夠使用從Admin控制器而來的數(shù)據(jù)的
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 亚洲国产精品人人做人人爱| 最新国产成人精品2024| 国产三级精品三级| 精品一区二区三区东京热| 国产精品无码v在线观看| 国产精品国色综合久久| 天天视频国产精品| 国产精品第1页| 91麻豆国产福利精品| 人妻少妇看A偷人无码精品| 久久久久人妻精品一区| 精品国产一区二区三区AV性色| 国产精品一区二区久久| 亚洲av无码乱码国产精品 | 杨幂国产精品福利在线观看| 自拍偷自拍亚洲精品被多人伦好爽 | 国产玖玖玖九九精品视频| 久久久91精品国产一区二区三区| 久久亚洲精品国产精品| 亚洲国产精品成人久久蜜臀 | 热re99久久6国产精品免费| 亚洲国产精品狼友中文久久久| 久久精品亚洲欧美日韩久久| 国产精品五月天强力打造| 国产精品VIDEOSSEX久久发布| 欧美亚洲国产精品久久蜜芽| 99精品国产丝袜在线拍国语 | 午夜精品一区二区三区免费视频| 日本精品自产拍在线观看中文 | 日韩人妻无码精品久久免费一| 亚洲精品无码久久久久AV麻豆| 欧美日韩精品| 免费精品国自产拍在线播放| 精品乱子伦一区二区三区高清免费播放 | 国产午夜精品一区二区三区不卡| 91精品国产成人网在线观看| 精品四虎免费观看国产高清午夜 | 成人精品一区二区三区电影黑人| 四虎国产精品免费久久5151| 亚洲天堂久久精品| 人精品影院|