I have a scripts on C# that make a list of strings. I want to create a JS script that activates on click on button and doing a C# script with getting a list of strings. I want to use RPC
Assistant
To create a JavaScript (JS) script that activates on a button click and triggers a C# script to retrieve a list of strings using Remote Procedure Call (RPC), you can utilize ASP.NET Core for the server-side C# code and JavaScript code for the client-side web interaction. Below, I will provide a simplified example of how you can achieve this.
1. Setting Up ASP.NET Core Web API
First, let's create a simple ASP.NET Core Web API that provides an endpoint to retrieve a list of strings.
Step 1: Create a New ASP.NET Core Web API Project
Using the .NET CLI, you can create a new project:
bash
dotnet new webapi -n MyApi
cd MyApi
Step 2: Create the C# Controller
In the Controllers folder, create a new controller named StringsController.cs.
csharp
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace MyApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class StringsController : ControllerBase
{
[HttpGet]
public ActionResult<List<string>> GetStrings()
{
var stringsList = new List<string>
{
"String 1",
"String 2",
"String 3"
};
return Ok(stringsList);
}
}
}
Step 3: Run the API
Make sure to run the API so that it listens for incoming requests.
bash
dotnet run
2. Creating the JavaScript Client
Now that the C# API is set up, let’s create a simple HTML page that includes JavaScript to make an RPC call.
Step 1: Create an HTML file
Create an index.html file in the root directory of your project.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String List Fetcher</title>
</head>
<body>
<h1>Fetch Strings from C# API</h1>
<button id="fetchButton">Fetch Strings</button>
<ul id="stringList"></ul>
<script>
document.getElementById('fetchButton').addEventListener('click', fetchStrings);
async function fetchStrings() {
try {
const response = await fetch('https://localhost:5001/strings'); // Adjust the URL if necessary
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
displayStrings(data);
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
}
}
function displayStrings(strings) {
const stringList = document.getElementById('stringList');
stringList.innerHTML = ''; // Clear existing strings
strings.forEach(str => {
const listItem = document.createElement('li');
listItem.textContent = str;
stringList.appendChild(listItem);
});
}
</script>
</body>
</html>
Step 2: Serve the HTML File
You can serve the HTML file using a simple HTTP server, or you can integrate it with your ASP.NET Core application by adding the static files support.
To add static files support in ASP.NET Core, you can follow these steps:
Add the following using directive to your Startup.cs or Program.cs.
csharp
using Microsoft.AspNetCore.StaticFiles;
Update your Configure method in Startup.cs or Program.cs:
csharp
app.UseStaticFiles();
3. Running the Project
After setting everything up:
Run the ASP.NET Core API (dotnet run).
Open the index.html file in a browser (or navigate to the root of your ASP.NET Core app).
Click the "Fetch Strings" button, and the list of strings from the C# API should be displayed.
Note
Make sure to run your ASP.NET Core app using HTTPS if you're fetching from https://localhost:5001 or adjust the URL accordingly for HTTP.
You can also implement error handling and loading states in the JavaScript code for better user experience.
This example provides a basic structure. Depending on your needs, you might want to modify or expand the functionality from here.