ASP.NET 5系列教程 (二):Hello World

來源:互聯網
上載者:User

標籤:

本篇文章內容比較基礎,主要是向大家展示如何建立一個 ASP.NET 5 工程,主要包含內容如下:

  • 建立ASP.NET 5 工程
  • 添加 Todo 控制器
  • 安裝 K Version Manager
  • 執行 EF 遷移

開啟Visual Studio 2015 Preview。選擇 ”File” 菜單,選擇New > Project。

在New Project 對話方塊中,點擊Templates > Visual C# > Web,選擇ASP.NET Web Application 工程模板。命名工程為"TodoList",點擊OK。

添加Todo 控制器

1. 下載完整工程completed project。

2. 在工程解決方案管理器中,右鍵點擊Controllers 檔案夾> Add > Exiting Item。輸入下載工程中的TodoController.cs 檔案路徑。

3. 使用相同的方法添加 Models\TodoItem.cs 和Models\TodoItemEditModel.cs 檔案到 Models 檔案夾。

4. 在Views下建立ToDo 檔案夾。使用同樣方法,添加Views\ToDo 檔案夾下所有View檔案到Views\Todo 檔案夾。

5. 更改Views\Shared\_Layout.cshtml 檔案下的ActionLink 調用Todo 控制器:

<!DOCTYPE html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>@ViewBag.Title - Todo</title><link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css" /><link rel="stylesheet" href="~/css/site.css" /></head><body><div class="navbar navbar-inverse navbar-fixed-top"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>@Html.ActionLink("Todo app", "Index", "Todo", new { area = "" }, new { @class = "navbar-brand" })</div><div class="navbar-collapse collapse"><ul class="nav navbar-nav">@* Markup removed for brevity *@</body></html>

6. 添加 DbSet 包含TodoItem 模型到Models\IdentityModels.cs 檔案。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>{private static bool _created = false;public DbSet<TodoItem> TodoItems { get; set; }// Code removed for brevity.}

7. 運行app 點擊Todo app 連結。你會遇到以下錯誤資訊 (將在後續文章中解決這個問題)。


A database operation failed while processing the request.
SqlException: Invalid object name ‘TodoItem‘.
There are pending model changes for ApplicationDbContext
Scaffold a new migration for these changes and apply them to the database from the command line
> k ef migration add [migration name]
> k ef migration apply

安裝K Version Manager (KVM)

1. 以管理員身份運行Visual Studio 命令提示工具。

2. 執行以下指令:


@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString(‘https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1‘))"


以上指令將為目前使用者安裝KVM。

3. 退出Visual Studio 命令提示工具,重新使用管理員身份運行Visual Studio命令提示工具(你需要使用新的命令提示工具擷取已經更新的路徑環境)。

4. 使用以下指令升級KVM :

KVM upgrade
現在已經可以運行 EF 遷移了。.

執行EF 遷移

1. 在管理員命令提示表單中,更改當前操作路徑為工程檔案夾,工程檔案夾下包含project.json 檔案。

2. 在命令提示表單中運行以下指令:

 

k ef migration add initial
k ef migration apply
ef migration add initial

指令將以<date>_<migration name>.cs 命名形式添加一個遷移檔案,遷移檔案中包含了遷移代碼that adds the TodoItem DbSet. TheMigrations\ApplicationDbContextModelSnapshot.cs 檔案file will be updated to include instructions to create theTodoItem entity.

builder.Entity("TodoList.Models.TodoItem", b => {    b.Property<int>("Id")   .GenerateValuesOnAdd();    b.Property<bool>("IsDone");    b.Property<int>("Priority");    b.Property<string>("Title");   b.Key("Id");});

· 運行應用,點擊Todo app 連結。點擊Create New Todo 連結建立一個新的Todo 項。

關於Todo 控制器

Todo 控制器類似於建立了一個帶有異常的ASP.NET MVC 5 工程。在這個樣本中,資料內容被注入到Todo 控制器. 你可以從連結中瞭解更多資訊:Dependency Injection in ASP.NET vNext。

以上步驟建立了一個新的ASP.NET 5.0 工程,在下一篇文章中我們將講述View Components 的使用方法。敬請期待.

 

原文地址:View components and Inject in ASP.NET MVC 6

ASP.NET 5系列教程 (二):Hello World

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.