Create an ASP.NET Core Application with React and Add TypeScript in Visual Studio
Hello everyone, I'm Hafizs Fauzi Ritonga
In this article, I'll guide you through creating a simple ASP.NET Core application with ReactJS and TypeScript in Visual Studio. Before we begin, here’s the environment we need to set up.
Prerequisites
Creating an ASP.NET Core Application with React in Visual Studio
Creating the Frontend Application
Configuring Project Properties
Starting the Project
Press F5 or select the Start button at the top of the window to start the application. Two commands will appear:
The React application will appear and connect through the API. If you do not see the application, check for troubleshooting tips.
Publishing the Project
app.UseDefaultFiles();
app.UseStaticFiles();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
7. To publish, right-click the ASP.NET Core project, select Publish, and choose the option that matches your publishing scenario, such as Azure, publish to folder, etc.
If successful, click Navigate to view the publishing results.
Then double-click the .exe file to see your simple React application successfully published.
Recommended by LinkedIn
Creating an ASP.NET Core Application with TypeScript in Visual Studio
Now, we will create a new MVC-based project where TypeScript will be added. Follow these steps:
2. Install the TypeScript Package:
3. Add TypeScript Configuration File:
4. Configure tsconfig.json: After adding the tsconfig.json file, you will see the default configuration. Change the default configuration to the following:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./wwwroot/js", // Tempat output file JavaScript hasil kompilasi
"strict": true,
"esModuleInterop": true
},
"include": [
"Models/scripts/**/*" // Mengarahkan TypeScript untuk mencari file di dalam folder Models/scripts
],
"exclude": [
"node_modules",
"wwwroot/js"
]
}
5. Add TypeScript Code:
6 Add TypeScript Code: Open the newly created main.ts file and add the following TypeScript code:
// Fungsi yang dipanggil saat tombol diklik
function TSButton() {
let user = new Student("Hafizs", "Fauzi", "Ritonga");
document.getElementById("ts-example")!.innerHTML = greeter(user);
}
// Kelas Student dengan properti fullName dan constructor untuk inisialisasi
class Student {
fullName: string;
// Constructor menerima firstName, middleInitial, dan lastName sebagai parameter
constructor(public firstName: string, public middleInitial: string, public lastName: string) {
// Menggabungkan firstName, middleInitial, dan lastName untuk membuat fullName
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
// Interface Person dengan properti firstName dan lastName
interface Person {
firstName: string;
lastName: string;
}
// Fungsi greeter yang menerima parameter bertipe Person dan mengembalikan string salam
function greeter(person: Person) {
// Mengembalikan string salam yang memuat nama depan dan nama belakang
return "Hello, " + person.firstName + " " + person.lastName;
}
// Membuat instance Student dengan nama Fred
let user = new Student("Hafizs", "Fauzi", "Ritonga");
Visual Studio provides IntelliSense support for TypeScript. To try it out, remove the lastName part, then type a dot (.) and observe the updates from IntelliSense.
Modify the Views:
<div id="ts-example">
<br />
<button type="button" class="btn btn-primary btn-md" onclick="TSButton()">
Click Me
</button>
</div>
Open Views > Shared, then open the _Layout.cshtml file. Add the following script reference before @RenderSectionAsync("Scripts", required: false):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - My ASP.NET Core Application</title>
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<h1>Welcome to My Application</h1>
</header>
<nav>
<!-- Navigation bar or menu -->
</nav>
<!-- Render the main content of the view -->
<main role="main" class="container">
@RenderBody() <!-- Placeholder for the content of the view -->
</main>
<footer>
<p>© 2024 - My ASP.NET Core Application</p>
</footer>
<script src="~/js/main.js"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
8. Build the Application: Select Build > Build Solution to build the application. Open the wwwroot/js folder to see two new files: app.js and app.js.map, which are generated by the TypeScript compiler.
9.Run the Application: Press F5 or select Debug > Start Debugging to run the application. The application will open in the browser.
By following these steps, you can create an ASP.NET Core MVC application that uses TypeScript in Visual Studio.
I hope this article is helpful! If you have any questions or issues, feel free to ask.