您好,登錄后才能下訂單哦!
現在,ASP.NET MVC 6 支持注入類到視圖中,和VC類不同的是,對類是公開的、非嵌套或非抽象并沒有限制。在這個例子中,我們創建了一個簡單的類,用于統計×××事件、已完成事件和平均優先級的服務。
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);
}
}
}
@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)
<h4>Stats</h4>
<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">
<h2>ASP.NET vNext</h2>
</div>
<div class="row">
<div class="col-md-4">
@if (Model.Count == 0)
{
<h5>No Todo Items</h5>
}
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)
<h4>Stats</h4>
<ul>
<li>Items: @await Statistics.GetCount()</li>
<li>Completed:@await Statistics.GetCompletedCount()</li>
<li>Average Priority:@await Statistics.GetAveragePriority()</li>
</ul>
</div>
</div>
// 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.
數據庫服務器是一個寶貴的資源。最好使用現有服務器進行測試和開發。然而由于沒有密碼校驗機制,密碼輸入錯誤時不會有錯誤提示,只有在應用實際訪問數據庫時才會報錯。
原文鏈接:http://www.asp.net/vnext/overview/aspnet-vnext/vc#inj
ASP.NET 5系列教程 (一):領讀新特性
ASP.NET 5系列教程 (二):Hello World
ASP.NET 5系列教程 (三):view components介紹
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。