Manage two spa in a single asp net core 3 1 web api one in the root and the oth

Introduction

ASP.NET is a popular programming language used for building web . In this article, we will discuss how to manage two Single Page Applications (SPA) in a single ASP.NET Core 3.1 Web API. We will explore the scenario one SPA is located in the root directory and the other SPA is located in a different directory.

Setting up the Project

To begin, let's create a new ASP.NET Core 3.1 Web API project. Open Studio and select “Create a new project”. Choose the ASP.NET Core Web Application template and select the API project type. Give your project a name and click “Create”.

Once the project is created, open the Startup.cs file. This file contains the configuration for our application. We need to make some changes to support multiple SPAs.

Configuring the Startup.cs File

In the Configure method of the Startup.cs file, add the code:


app.UseDefaultFiles();
app.UseStaticFiles();

This code enables serving default and static files from the wwwroot folder. It allows us to our SPAs from the root directory.

Adding the First SPA

Let's add the first SPA to our project. Create a new folder in the root directory of your project and name it “spa1”. Inside this folder, add the necessary files for your SPA.

Now, let's configure the routing for our first SPA. In the Configure method of the Startup.cs file, add the following code:


app.UseSpa(spa =>
{
    spa..SourcePath = "spa1";
});

This code tells ASP.NET to serve the SPA located in the “spa1” folder.

Adding the Second SPA

Similarly, let's add the second SPA to our project. Create folder in the root directory of your project and name it “spa2”. Inside this folder, add the necessary files for your second SPA.

In the Configure method of the Startup.cs file, add the following code:


app.Map("/spa2", spa =>
{
    spa.UseSpa(spa =>
    {
        spa.Options.SourcePath = "spa2";
    });
});

This code tells ASP.NET to serve the SPA located in the “spa2” folder when the URL contains “/spa2”.

Conclusion

In this article, we have discussed how to manage two SPAs in a single ASP.NET Core 3.1 Web API. We have seen how to configure the Startup.cs file to serve the SPAs from different directories. By following , you can easily manage multiple SPAs in your ASP.NET Core project.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents