ASP. NET 5 Series tutorial (iv): adding services and publishing to a view apply to public cloud

Source: Internet
Author: User
Tags actionlink

Add a service to a view

Now, ASP. NET MVC 6 supports injecting classes into views, unlike VC classes, where classes are exposed, non-nested, or non-abstract and have no limitations. In this example, we create a simple class that counts the services of the agent event, the completed event, and the average priority.

1. Add the name Servicesfolder, under which you add a name called StatisticsService.cs's Class:

The Statisticsservice class code is designed as follows:

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 (
          true));
    }
     Public Async task<double> getaveragepriority ()
    {
      return await Task.fromresult (
          Db. Todoitems.average (x =
                     (double?) x.priority)?? 0.0);
    }
  }
}
2. Update the index view Injection agent data, add the following code at the top of the file to declare the injected service:

@inject TodoList.Services.StatisticsService Statistics

Add Tag call Statisticsservice:

<div> @Html. ActionLink ("Create New Todo", "Create", "todo") </div>
    </div>
     
    class= "col-md-4" >
        @await Component.invokeasync ("prioritylisttrue")
      
      <ul>
        <li>items: @await statistics.getcount () </li>
        <li>completed: @await statistics.getcompletedcount () </li>
        <li>average Priority: @await statistics.getaveragepriority () </li>
      </ul>
    </div>
</div>

The following is the complete code for the file:

@inject TodoList.Services.StatisticsService Statistics
@{
    Viewbag.title = "Home page";
}
class= "Jumbotron" >
    
</div>
class= "row" >
    class= "col-md-4" >
        @If (Model.count = = 0)
        {
            
        }
        Else
        {
            <table>
                <tr><th>TODO</th><th></th></tr>
                inch Model)
                {
                    <tr>
                        <td> @todo. Title </td>
                        <td>
                            @Html. ActionLink ("details", "details", "todonew {id = todo.") Id}) |
                            @Html. ActionLink ("edit", "edit", "todonew {id = todo.") Id}) |
                            @Html. ActionLink ("delete", "delete", "todonew {id = todo". ID})
                        </td>
                    </tr>
                }
            </table>
                            }
        <div> @Html. ActionLink ("Create New Todo", "Create", "todo") </div>
    </div>
     
    class= "col-md-4" >
        @await Component.invokeasync ("prioritylisttrue")
      
      <ul>
        <li>items: @await statistics.getcount () </li>
        <li>completed: @await statistics.getcompletedcount () </li>
        <li>average Priority: @await statistics.getaveragepriority () </li>
      </ul>
    </div>
</div>

3. InStartup.csRegister the Statisticsservice class in the file:
This method gets called by the runtime.
 public void configureservices (iservicecollection services)
{
   to the Services container.
  Services. Addentityframework (Configuration)
      . Addsqlserver ()
      . Adddbcontext<applicationdbcontext> ();
   to the Services container.
  Services. Adddefaultidentity<applicationdbcontext, ApplicationUser, identityrole> (Configuration);
   to the Services container.
  Services. Addmvc ();
  Services. Addtransient<todolist.services.statisticsservice> ();
}

The following are:

Publish app toPublicCloud

To publish the app to the public cloud, you need to apply for a Microsoft Azure account, and if not, you can register with the following link: Activate your MSDN subscriber benefits or sign up for a free trial.

1. Right-clickToDoListEngineering >Publish

2. In the Publish dialog box, clickMicrosoft Azure Websitesand log in to the public cloud account.

3. ClickNew.

4. Enter site name and region. If you have not created a data server before, you need to create a new one, otherwise you can use the original database server.


The database server is a valuable resource. It is best to test and develop with an existing server. However, because there is no password check mechanism, the password input error will not have an error, only when the application actually access the database will be errors.

5. InConnectiontab, click >Next.

6. InSettingstab, select the KRE version.

7. ClickPublish.

8. Now that your app is published to the public cloud, here's the following.

Original link: Http://www.asp.net/vnext/overview/aspnet-vnext/vc#inj

Series Articles Directory:
    • ASP. 5 Series Tutorial (i): bribed new features

    • ASP. 5 Series Tutorial (ii): Hello World

    • ASP 5 Series Tutorial (iii): View Components Introduction

ASP. NET 5 Series tutorial (iv): adding services and publishing to a view apply to public cloud

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.