ASP.NET 5系列教程 (四):向视图中添加服务和发布应用到公有云

简介:

向视图中添加服务

现在,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>();
}

以下是效果图:

image

发布应用到公有

发布应用到公有云,你需要申请 Microsoft Azure 帐号,如果没有,可以通过以下链接注册:activate your MSDN subscriber benefits 或 sign up for a free trial.

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



本文转自 powertoolsteam 51CTO博客,原文链接:http://blog.51cto.com/powertoolsteam/1602786,如需转载请自行联系原作者

相关文章
|
1月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
29 7
|
1月前
|
SQL 关系型数据库 数据库
七天.NET 8操作SQLite入门到实战详细教程(选型、开发、发布、部署)
七天.NET 8操作SQLite入门到实战详细教程(选型、开发、发布、部署)
|
2月前
|
存储 NoSQL Redis
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
|
2月前
|
敏捷开发 设计模式 开发者
【揭秘终极利器】AgileEAS.NET:服务定位器模式的魔法,如何让企业级软件开发瞬间提速?揭秘背后的技术奥秘与实战指南!
【8月更文挑战第16天】AgileEAS.NET是基于DotNet的企业级敏捷开发平台,其服务定位器模式助力构建高度解耦系统。通过全局服务目录动态查找服务,避免硬编码依赖。在AgileEAS.NET中,服务定位器以静态类形式封装服务注册与检索功能。示例展示了如何注册与获取服务实例,如在`UserController`中通过服务定位器使用`IUserService`。此模式整合到框架生命周期管理,便于各处获取服务实例,提升开发效率。然而,应适度使用并考虑依赖注入容器以增强代码可维护性和可测试性。
60 4
|
2月前
|
开发框架 .NET API
在IIS上部署ASP.NET Core Web API和Blazor Wasm详细教程
在IIS上部署ASP.NET Core Web API和Blazor Wasm详细教程
136 3
|
2月前
|
安全 Java 网络安全
Android远程连接和登录FTPS服务代码(commons.net库)
很多文章都介绍了FTPClient如何连接ftp服务器,但却很少有人说如何连接一台开了SSL认证的ftp服务器,现在代码来了。
80 2
|
2月前
|
开发框架 .NET 编译器
【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
|
2月前
|
开发框架 .NET 开发工具
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
|
2月前
|
开发框架 监控 .NET
【Azure 应用程序见解】在Docker中运行的ASP.NET Core应用如何开启Application Insights的Profiler Trace呢?
【Azure 应用程序见解】在Docker中运行的ASP.NET Core应用如何开启Application Insights的Profiler Trace呢?
|
3月前
|
开发框架 搜索推荐 前端开发
【.NET全栈】ASP.NET开发Web应用——Web部件技术
【.NET全栈】ASP.NET开发Web应用——Web部件技术