ASP.NET 5系列教程 (四):向視圖中添加服務和發布應用到公用雲端

來源:互聯網
上載者:User

標籤:asp.net 5   mvc 6   microsoft azure   

??向視圖中添加服務

現在,ASP.NET MVC 6 支援注入類到視圖中,和VC類不同的是,對類是公開的、非嵌套或非抽象並沒有限制。在這個例子中,我們建立了一個簡單的類,用於統計代辦事件、已完成事件和平均優先順序的服務。

1. 添加命名為Services 的檔案夾,在該檔案夾下添加名稱為 StatisticsService.cs 的類:

StatisticsService 類代碼設計如下:

using System.Linq;
using System.Threading.Tasks;
using TodoList.Models;

 
namespace TodoList.Services
{
  public class StatisticsService
  {
    private readonly ApplicationDbContext db;

 
    public StatisticsService(ApplicationDbContext context)
    {
      db = context;
    }

 
    public async Task<int> GetCount()
    {
      return await Task.FromResult(db.TodoItems.Count());
    }

 
    public async Task<int> GetCompletedCount()
    {
      return await Task.FromResult(
          db.TodoItems.Count(x => x.IsDone == true));
    }

 
    public async Task<double> GetAveragePriority()
    {
      return await Task.FromResult(
          db.TodoItems.Average(x =>
                     (double?)x.Priority) ?? 0.0);
    }
  }
}
2. 更新Index 視圖注入代辦事項資料,在檔案頂部添加以下代碼聲明注入的服務:

@inject TodoList.Services.StatisticsService Statistics

委任標記調用 StatisticsService:

<div>@Html.ActionLink("Create New Todo", "Create", "Todo") </div>
    </div>
     
    <div class="col-md-4">
        @await Component.InvokeAsync("PriorityList", 4, true)

 
      <h3>Stats</h3>
      <ul>
        <li>Items: @await Statistics.GetCount()</li>
        <li>Completed:@await Statistics.GetCompletedCount()</li>
        <li>Average Priority:@await Statistics.GetAveragePriority()</li>
      </ul>
    </div>
</div>

 

以下是該檔案的完整代碼:

@inject TodoList.Services.StatisticsService Statistics
@{
    ViewBag.Title = "Home Page";
}

 
<div class="jumbotron">
    <h1>ASP.NET vNext</h1>
</div>

 
<div class="row">
    <div class="col-md-4">
        @if (Model.Count == 0)
        {
            <h4>No Todo Items</h4>
        }
        else
        {
            <table>
                <tr><th>TODO</th><th></th></tr>
                @foreach (var todo in Model)
                {
                    <tr>
                        <td>@todo.Title </td>
                        <td>
                            @Html.ActionLink("Details", "Details", "Todo", new { id = todo.Id }) |
                            @Html.ActionLink("Edit", "Edit", "Todo", new { id = todo.Id }) |
                            @Html.ActionLink("Delete", "Delete", "Todo", new { id = todo.Id })
                        </td>
                    </tr>
                }
            </table>
                            }
        <div>@Html.ActionLink("Create New Todo", "Create", "Todo") </div>
    </div>
     
    <div class="col-md-4">
        @await Component.InvokeAsync("PriorityList", 4, true)

 
      <h3>Stats</h3>
      <ul>
        <li>Items: @await Statistics.GetCount()</li>
        <li>Completed:@await Statistics.GetCompletedCount()</li>
        <li>Average Priority:@await Statistics.GetAveragePriority()</li>
      </ul>
    </div>
</div>

 

3. 在 Startup.cs 檔案中註冊StatisticsService 類:
// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
  // Add EF services to the services container.
  services.AddEntityFramework(Configuration)
      .AddSqlServer()
      .AddDbContext<ApplicationDbContext>();

 
  // Add Identity services to the services container.
  services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration);

 
  // Add MVC services to the services container.
  services.AddMvc();

 
  services.AddTransient<TodoList.Services.StatisticsService>();
}

以下是:

發布應用到公用雲端

發布應用到公用雲端,你需要申請 Microsoft Azure 帳號,如果沒有,可以通過以下連結註冊:activate your MSDN subscriber benefits 或 sign up for a free trial.

1. 右鍵點擊 TodoList 工程> 發布

2. 在發布對話方塊中,點擊 Microsoft Azure Websites 並登陸公用雲端帳號。

3. 點擊 New。

4. 輸入site name 和region。如果你之前沒有建立過資料服務器,需要建立,否則可以使用原有的資料庫伺服器。


資料庫伺服器是一個寶貴的資源。最好使用現有伺服器進行測試和開發。然而由於沒有密碼校正機制,密碼輸入錯誤時不會有錯誤提示,只有在應用實際訪問資料庫時才會報錯。

5. 在Connection 標籤中點擊> Next。

6. 在Settings 標籤中,選擇 KRE 版本。

7. 點擊 Publish。

 

8. 好了,至此你的應用就發布到公用雲端了,以下是。

原文連結:http://www.asp.net/vnext/overview/aspnet-vnext/vc#inj

 

系列文章目錄:
  • ASP.NET 5系列教程 (一):領讀新特性

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

  • ASP.NET 5系列教程 (三):view components介紹

ASP.NET 5系列教程 (四):向視圖中添加服務和發布應用到公用雲端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.