Thursday, September 7, 2023

Convert a "Class Library" or "Console" project into "Web Application" c# dotnet core on Ubuntu OS

 Step 1: Create a repository

Prerequisite: Signup for a GitHub account and Login to your GitHub account.

Create a new repository →

  • Repository Name : ClassLibToWebApp

  • Add Description

  • Choose Public from Public/Private Option

  • Add a Readme.md by checking the Checkbox.

  • Click on the create repository button.

  • Confirm repository created on seeing the Repository created successfully message

Step 2: Clone the repository to local machine:

  • Go to GitHub Server → Open your repository  → Click on Code button

  • Copy the SSH



Step3: Make a directory in local machine WSL

  • Open WSL terminal

  • Ensure you are in home directoryMake a directory 2023 - mkdir 2023

  • Change directory to 2023 - cd 2023

  • Make a directory 202309 - mkdir 202309

  • Change directory to 202309- cd 202309

  • Make a directory 20230901 - mkdir 20230901

  • Change directory to 20230901 - cd 20230901

Step4: Go to the WSL terminal and Choose the path to clone the repository.

  • Make sure we are inside 2023/202309/20230901 directory

  • Use the command git clone to clone the repository.

  • git clone git@github.com:laakansolutiondevelopers/ClassLibToWebApp

Step5: Change the directory to the repository folder.

cd ClassLibToWebApp

Step6: Create a branch.

  • After cloning the repository, Create a branch in it using the git branch command.

git branch users/all/20230901_Initial_Setup

  • To Change the branch use the command below

git checkout users/all/20230901_Initial_Setup

Step7: Create a solution file.

  • Create a solution file using the command given below,

dotnet new solution

Step8: Add a gitignore file.

dotnet new gitignore

Step9: Create a Classlib project as LS.Proj

dotnet new classlib -o LS.Proj

  • Once we run this command, we are able to see the files below : use ll command

  1. Class1.cs

  2. LS.Proj.csproj

  3. Obj


Step 10: For Classlib project Add a gitignore


  • Copy the gitignore from repository to the Project folder

  • cp .gitignore LS.Proj/

Step 11:  Add solution for classlib project.

  • dotnet sln add LS.Proj/LS.Proj.csproj

Step 12: Change the classlib project to web app by adding and modify the project.

(Use the Visual Studio Code Editor)

Type the command code . to redirect to the Visual Studio Code Editor.

 

Step 13: Go to LS.Proj.csproj file:


 update <Project Sdk="Microsoft.NET.Sdk"> to <Project Sdk="Microsoft.NET.Sdk.Web">


<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>

    <TargetFramework>net7.0</TargetFramework>

    <ImplicitUsings>enable</ImplicitUsings>

    <Nullable>enable</Nullable>

  </PropertyGroup>

</Project>

→ In above Screenshot for web application, add .web extension at the end<Project Sdk="Microsoft.NET.Sdk.Web">

 Step 14 : Rename Class1.cs file to StartHere.cs (Use F2 button to rename file)

Change the class name to StartHere.


Example of Project is given below:


Create a Main Method

namespace LS.Proj;

public class StartHere

{

public static void Main(string[] args)

{

Console.WriteLine("Start of Web Application");

WebApplication webApp = WebApplication.Create(args);

webApp.MapGet("/", () => HelloWorld());

webApp.MapGet("/ap", () => HelloWorld("Akia"));

webApp.MapGet("/rr", () => HelloWorld("Rangan"));

webApp.MapGet("/sc", () => HelloWorld("Santhoz"));

webApp.Run();

Console.WriteLine("End of Web Application");

}

public static string HelloWorld(string name = null)

{

if (string.IsNullOrEmpty(name))

{

return "You are at HelloWorld Path.";

}

else

{

return $"you are {name}.. Welcome to Web Application development";

}

}

}

 15. Build the Project

dotnet build

16. Run the Project

dotnet run --project LS.Proj

17. Output

Start of Web Application
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000

 


In browser :

paste the link copied at URL box: http://localhost:5000

 To open developer tools:

Use the shortcut → Ctrl+Shift+i

 

Quiz:

  1. What is classlib?

  2. What is Web Application?

  3. What is a Web Site?

  4. Which template can you use to create a web application in dotnet core?

  5. What is a command to create classlib project?

  6. How to create a solution file?

  7. What is URL?

  8. What is endpoint in URL?

  9. SDK stands for?

  10. How to compile a C# project?


To check your answer please visit: Quiz Answers



Glossary of Terms:


Terms

Definitions

Reference

dotnet

Dotnet is a multipurpose command line tools for generating applications based on predefined templates

https://dotnet.microsoft.com/en-us/learn

Template

Project templates like ‘classlib’ class library are list of available templates for creating specific types of dotnet projects

https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-new-sdk-templates

Dotnet Core

Microsoft’s framework for building applications which can run on multiple operating systems.

https://dotnet.microsoft.com/en-us/ 

MVC

Model View Controller is a popular pattern for structuring web applications

https://www.geeksforgeeks.org/mvc-framework-introduction/

Mapget 

It’s a method used to track the path in URL

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.endpointroutebuilderextensions.mapget?view=aspnetcore-7.0


No comments:

Post a Comment

How to Host Your Angular App on GitHub Pages

Are you looking to showcase your Angular project to the world but unsure about hosting options? GitHub Pages offers a simple and free soluti...