现在,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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。