
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open Visual Studio 2017.
- Now, open the solution from the previous recipe. Click File | Open | Open Project/Solution, or press Ctrl + Shift + O, and select the Chapter1.StandardLib solution.
- Now click on the Chapter1.Library solution label. Click File | Add | New Project.
- In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane. Select Web and select ASP.NET Core Web Application from the right-hand pane:

- In the Name: text box, type Chapter1.StandardLib.AspNetCore as the name of the project and leave the Location: as it is:

- Click OK.
- Now, in the New ASP.NET Core Web Application dialog box, select .NET Core from the first drop-down list and ASP.NET Core 2.0 from the second drop-down list. Finally, select Web Application (Model-View-Controller) from the templates list:

- Leave the defaults as they are and Click OK.
- Now, the Solution Explorer should look like this:

- Select the Chapter1.StandardLib.AspNetCore project, right-click, and select Set as Startup Project.
- Now hit F5 for a test run. If everything is running smoothly, you should see this default ASP.NET Core template running on your default browser:

Default ASP.NET Core template running on your default browser
- Let's close the browser and add our .NET Standard class library as a reference. To do this, expand the Chapter1.StandardLib.AspNetCore project tree and select Dependencies.
- Right-click on the Dependencies label and select Add Reference.
- Under the Reference Manager dialog box, click on the Projects label in the left-hand pane. In the middle pane, check the Chapter1.StandardLib.HelloUniverse project and click OK.

- Let's expand the Controllers folder and double-click HomeController.cs.
- In HomeController.cs, add this code right next to the last line of the using directive block:
using Chapter1.StandardLib;
- Now, inside the About() action, add the following code block after the ViewData["Message"] line (by default, this is after line 21 in the default template):
var myName = "Fiqri Ismail";
var helloMessage = new HelloUniverse();
ViewData["HelloMessage"] = helloMessage.SayHello(myName);
- Now expand the Views folder. Again, expand the Home folder as well.
- Double-click on About.cshtml.
- At the end of About.cshtml, add the following code:
<p>@ViewData["HelloMessage"]</p>
- Now press F5 to see it in action.
- You will see the default ASP.NET Core template in the browser. Now click About to view the About.cshtml page:

About.cshtml page
- Excellent, now you have used a .NET Standard 2.0 library with an ASP.NET Core 2.0 web application.