On Mac, yeoman is developed to build an Asp.net core project and implement hierarchical reference. yeomancore

Source: Internet
Author: User

On Mac, yeoman is developed to build an Asp.net core project and implement hierarchical reference. yeomancore
1. Yeoman?

Yeoman is an automated scaffold tool. It provides many generator, which is equivalent to the template of Visual Studio and used to initialize the project. I will not talk more about it. I can't finish writing it all over again. Let's take a look.

Http://yeoman.io/

2. Install yeoman

Install npm before installing yeoman. Npm is a JavaScript package management tool, which is generally used with nodejs. Https://docs.npmjs.com/getting-started/what-is-npm

Install yeoman using npm:

$npm install -g yo

The installation speed is relatively slow. Please use the npm image of registry Alibaba as much faster.

3. Install generator-aspnet

Generator-aspnet is the builder of asp.net core. We need to install it.

Generator-aspnet depends on the bower tool, so we need to install it together.

$npm install -g bower$npm install -g generator-aspnet

After installation, you can use yeoman.

4. Use yeoman

Before using this function, let's assume that our project is Coreyo, which is divided into two projects: Coreyo. Web and Coreyo. Services.

Let's create a new project's root directory Coreyo.

mkdir Coreyo
cd Coreyo
Use yeoman to create Coreyo. Web
yo aspnet

Yeoman will display an interface for you to select the type of the created Project

If you choose one, select the Web Application Basic Project.

Press enter, and then select the UI component to use.

Select Bootstrap and press Enter.

Enter the project name Coreyo. web, after you press enter, yeoman will help you generate all the files, including the default Controllers, Views, and JavaScript libraries. This is the same as the work we have done to create a new project using ViusalStudio.

Use VSCode to open the project folder.

Run it.

cd Coreyo.Webdotnet restoredotnet builddotnet run

Use yeoman to create Coreyo. Services

When developing projects, we must perform layers such as UI, Services, Data, and Models. If it is VS, right-click the solution to create a project, but VSCode obviously does not support it. We have to create it manually.

cd ..
yo aspnet

Go to the root directory and run the yo aspnet command to create a new project.

Select Library when selecting the project type, and enter the name Coreyo. Services. The file is automatically generated after you press Enter.

We use VSCode to open this project, rename Class1 as UserService, and add a GetUserName method.

namespace Coreyo.Services{    public class UserService    {        public static string GetUserName(string userId)        {            return "Agile.zhou";        }    }}

Then restore and build it. There is no error.

Coreyo. Web reference Coreyo. Services

But now Coreyo. Services and Coreyo. Web are independent, and VSCode does not have the reference function of VS. How can Coreyo. Web reference Coreyo. Services and call its method?

We open project. json of Coreyo. Web and add a reference to Coreyo. Services under the dependencies node.

"Coreyo. Services": "1.0.0"

Then we package Coreyo. Services

dotnet pack

After the package is successful, we switch to the Coreyo. Web directory and use restore to restore

cd ..cd Coreyo.Webdotnet restore

After the restoration is successful, call the GetUserName method of UserService in HomeController/Index.

When a user accesses the homepage, the user name Agile. zhou is printed on the console.

Then we build and Run Coreyo. Web

dotnet builddotnet run

Take a look at the console

It can be seen that our Services are successfully called.

Kun, ZZZZZzzzzzz...

Related Article

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.