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

向視圖中添加服務

如今,ASP.NET MVC 6 支持注入類到視圖中,和VC類不一樣的是,對類是公開的、非嵌套或非抽象並無限制。在這個例子中,咱們建立了一個簡單的類,用於統計代辦事件、已完成事件和平均優先級的服務。html

1. 添加命名爲Services 的文件夾,在該文件夾下添加名稱爲 StatisticsService.cs 的類:

StatisticsService 類代碼設計以下:數據庫

 System.Linq;
 System.Threading.Tasks;
 TodoList.Models;
 
 TodoList.Services
{
    StatisticsService
  {
      ApplicationDbContext db;
 
     StatisticsService(ApplicationDbContext context)
    {
      db = context;
    }
 
     async Task<int> GetCount()
    {
       await Task.FromResult(db.TodoItems.Count());
    }
 
     async Task<int> GetCompletedCount()
    {
       await Task.FromResult(
          db.TodoItems.Count(x => x.IsDone == ));
    }
 
     async Task<> GetAveragePriority()
    {
       await Task.FromResult(
          db.TodoItems.Average(x =>
                     (?)x.Priority) ?? 0.0);
    }
  }
}

2. 更新Index 視圖注入代辦事項數據,在文件頂部添加如下代碼聲明注入的服務:

@inject TodoList.Services.StatisticsService Statisticsc#

添加標記調用 StatisticsService:瀏覽器

<div>@Html.ActionLink("", "", "") </div>
    </div>
 
    <div ="">
        @await Component.InvokeAsync("", 4, )
 
      <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 = "";
}
 
<div ="">
    <h1>ASP.NET vNext</h1>
</div>
 
<div ="">
    <div ="">
        @ (Model.Count == 0)
        {
            <h4>No Todo Items</h4>
        }
else
        {
            <table>
                <tr><th>TODO</th><th></th></tr>
                @foreach (var todo  Model)
                {
                    <tr>
                        <td>@todo.Title </td>
                        <td>
                            @Html.ActionLink("", "", "",  { id = todo.Id }) |
                            @Html.ActionLink("", "", "",  { id = todo.Id }) |
                            @Html.ActionLink("", "", "",  { id = todo.Id })
                        </td>
                    </tr>
                }
            </table>
                            }
        <div>@Html.ActionLink("", "", "") </div>
    </div>
 
    <div ="">
        @await Component.InvokeAsync("", 4, )
 
      <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.
 void ConfigureServices(IServiceCollection services)
{
  // Add EF services  the services container.
  services.AddEntityFramework(Configuration)
      .AddSqlServer()
      .AddDbContext<ApplicationDbContext>();
 
  // Add Identity services  the services container.
  services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration);
 
  // Add MVC services  the services container.
  services.AddMvc();
 
  services.AddTransient<TodoList.Services.StatisticsService>();
}

如下是效果圖:asp.net

image

發佈應用到公有

發佈應用到公有云,你須要申請 Microsoft Azure 賬號,若是沒有,能夠經過如下連接註冊:activate your MSDN subscriber benefitssign up for a free trial.async

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

image

2. 在發佈對話框中,點擊 Microsoft Azure Websites 並登錄公有云賬號。

image

3. 點擊 New。

image

4. 輸入site name 和region。若是你以前沒有建立過數據服務器,須要新建,不然可使用原有的數據庫服務器。

image
數據庫服務器是一個寶貴的資源。最好使用現有服務器進行測試和開發。然而因爲沒有密碼校驗機制,密碼輸入錯誤時不會有錯誤提示,只有在應用實際訪問數據庫時纔會報錯。工具

image

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

image

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

image

7. 點擊 Publish。

 

8. 好了,至此你的應用就發佈到公有云了,如下是效果圖。

image

原文連接:http://www.asp.net/vnext/overview/aspnet-vnext/vc#inj開發工具

以上內容,介紹瞭如何向視圖中添加服務和發佈應用到公有云中,你們能夠將這些知識運用到實際開發中。開發時還能夠藉助一些工具。ComponentOne Studio for ASP.NET 是ASP.NET平臺上的一整套完備的開發工具包,用於在各類瀏覽器中建立和設計具備現代風格的Web應用程序。測試

系列文章目錄:

相關文章
相關標籤/搜索