ASP.NET 5系列教程 (四):向视图中添加服务和发布应用到公有云
现在,ASP.NET MVC 6 支持注入类到视图中,和VC类不同的是,对类是公开的、非嵌套或非抽象并没有限制。在这个例子中,我们创建了一个简单的类,用于统计×××事件、已完成事件和平均优先级的服务。
1. 添加命名为Services 的文件夹,在该文件夹下添加名称为 StatisticsService.cs 的类:StatisticsService 类代码设计如下:
usingSystem.Linq;
usingSystem.Threading.Tasks;
usingTodoList.Models;
namespaceTodoList.Services
{
publicclassStatisticsService
{
privatereadonlyApplicationDbContextdb;
publicStatisticsService(ApplicationDbContextcontext)
{
db=context;
}
publicasyncTask<int>GetCount()
{
returnawaitTask.FromResult(db.TodoItems.Count());
}
publicasyncTask<int>GetCompletedCount()
{
returnawaitTask.FromResult(
db.TodoItems.Count(x=>x.IsDone==true));
}
publicasyncTask<double>GetAveragePriority()
{
returnawaitTask.FromResult(
db.TodoItems.Average(x=>
(double?)x.Priority)??0.0);
}
}
}2. 更新Index 视图注入×××事项数据,在文件顶部添加以下代码声明注入的服务:
@inject TodoList.Services.StatisticsService Statistics
添加标记调用 StatisticsService:
<div>@Html.ActionLink("CreateNewTodo","Create","Todo")</div>
</div>
<divclass="col-md-4">
@awaitComponent.InvokeAsync("PriorityList",4,true)
<h4>Stats</h4>
<ul>
<li>Items:@awaitStatistics.GetCount()</li>
<li>Completed:@awaitStatistics.GetCompletedCount()</li>
<li>AveragePriority:@awaitStatistics.GetAveragePriority()</li>
</ul>
</div>
</div>
以下是该文件的完整代码:
@injectTodoList.Services.StatisticsServiceStatistics
@{
ViewBag.Title="HomePage";
}
<divclass="jumbotron">
<h2>ASP.NETvNext</h2>
</div>
<divclass="row">
<divclass="col-md-4">
@if(Model.Count==0)
{
<h5>NoTodoItems</h5>
}
else
{
<table>
<tr><th>TODO</th><th></th></tr>
@foreach(vartodoinModel)
{
<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("CreateNewTodo","Create","Todo")</div>
</div>
<divclass="col-md-4">
@awaitComponent.InvokeAsync("PriorityList",4,true)
<h4>Stats</h4>
<ul>
<li>Items:@awaitStatistics.GetCount()</li>
<li>Completed:@awaitStatistics.GetCompletedCount()</li>
<li>AveragePriority:@awaitStatistics.GetAveragePriority()</li>
</ul>
</div>
</div>
3. 在 Startup.cs 文件中注册StatisticsService 类://Thismethodgetscalledbytheruntime.
publicvoidConfigureServices(IServiceCollectionservices)
{
//AddEFservicestotheservicescontainer.
services.AddEntityFramework(Configuration)
.AddSqlServer()
.AddDbContext<ApplicationDbContext>();
//AddIdentityservicestotheservicescontainer.
services.AddDefaultIdentity<ApplicationDbContext,ApplicationUser,IdentityRole>(Configuration);
//AddMVCservicestotheservicescontainer.
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介绍
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。