Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
.NET Standard 2.0 Cookbook

You're reading from   .NET Standard 2.0 Cookbook Develop high quality, fast and portable applications by leveraging the power of .NET Standard Library

Arrow left icon
Product type Paperback
Published in May 2018
Publisher
ISBN-13 9781788834667
Length 394 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Ismail Ismail
Author Profile Icon Ismail
Ismail
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
1. Back to Basics FREE CHAPTER 2. Primitives, Collections, LINQ, and More 3. Working with Files 4. Functional Programming 5. XML and Data 6. Exploring Threading 7. Networking 8. To iOS with Xamarin 9. To Android with Xamarin 10. Let’s Fine-Tune Our Library 11. Packaging and Delivery 12. Deploying Index

Creating an ASP.NET Core-based web application to use the library


So far, we have tested the .NET Standard 2.0 class library with a Windows console application that runs under full .NET Framework version 4.6.1. In this recipe, we are going to create an ASP.NET Core 2.0 application. ASP.NET Core uses .NET Core, which is an open source, cross-platform supported .NET flavor. 

Getting ready

Let's get ready to create the ASP.NET Core application to use the .NET Standard library we have built in the previous recipe when we created the .NET Standard library. If you haven't followed that recipe, make sure you have completed it. We are going to use that solution and add the ASP.NET Core application to it. Also, make sure you have downloaded and installed the latest version of .NET Core Framework, which is available at http://www.dot.net/core

Open Visual Studio 2017 and open the solution we saved from the previous recipe. Click Build | Build Solution, or press Ctrl + Shift + B, and the solution should build successfully. Everything's ready for testing our class library. 

How to do it...

  1. Open Visual Studio 2017.
  2. 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. 
  1. Now click on the Chapter1.Library solution label. Click File | Add | New Project.
  2. 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:
  1. In the Name: text box, type Chapter1.StandardLib.AspNetCore as the name of the project and leave the Location: as it is:
  1. Click OK.
  1. 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:
  1. Leave the defaults as they are and Click OK.
  1. Now, the Solution Explorer should look like this: 
  1. Select the Chapter1.StandardLib.AspNetCore project, right-click, and select Set as Startup Project
  1. 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

  1. 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.
  2. Right-click on the Dependencies label and select Add Reference
  1. 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.
  1. Let's expand the Controllers folder and double-click HomeController.cs.
  2. In HomeController.cs, add this code right next to the last line of the using directive block:
      using Chapter1.StandardLib;
  1. 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);
  1. Now expand the Views folder. Again, expand the Home folder as well.
  2. Double-click on About.cshtml.
  3. At the end of About.cshtml, add the following code:
      <p>@ViewData["HelloMessage"]</p>
  1. Now press F5 to see it in action.
  1. You will see the default ASP.NET Core template in the browser. Now click About to view the About.cshtml page: 

About.cshtml page

  1. Excellent, now you have used a .NET Standard 2.0 library with an ASP.NET Core 2.0 web application. 

How it works...

Let's have a look what we did just now. From steps 1 to 9, we opened and previously built an existing solution containing .NET Standard 2.0 library code. Then, we added an ASP.NET Core project to that solution. In step 10, we told Visual Studio to execute the ASP.NET Core project when we hit F5 or started debugging. In step 11, we tested the default template of ASP.NET Core in a default browser. 

In steps 12 to 14, we added the reference to our ASP.NET Core application from the .NET Standard 2.0 class library. This allows you to access the library from an ASP.NET Core 2.0 web application. In step 16, we referenced the class library using the using directive. In step 17, we created a variable to hold the name and created an instance of the HelloUniverse class. 

Finally, we have stored the message from the SayHello() method in the ViewData collection. The ViewData collection allows you to transfer data from Controllers to Views. In steps 19 and 20, we opened the relevant view for the About() action, which is About.cshtml. Finally, in step 20, we added simple HTML code to display the stored value in ViewData in the HomeController class. As a last step, we executed the web application and tested it. 

You have been reading a chapter from
.NET Standard 2.0 Cookbook
Published in: May 2018
Publisher:
ISBN-13: 9781788834667
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images