How to host multiple .NET Core apps under the same URL?

25,560

Solution 1

You could use a reverse proxy for serving multiple ASP Net Core application on the same domain.
Using IIS I'm not quite sure but you need to install URL Rewrite and then follow the documentation provided by Microsoft (also this one should be helpful).
You can also use nginx using location and proxy_pass as follow:

...
Some nginx Configuration 
location /App1 {
        proxy_pass http://127.0.0.1:443;
    }
location /App2 {
        proxy_pass http://127.0.0.1:444;
    }
location /App3 {
        proxy_pass http://127.0.0.1:445;
    }
Other configurations...

And then, each time you want add another ASP Net application to your domain you'll need to add another location, run the application on a different point and restart nginx.

Solution 2

I got this working using the advice for Sub-applications here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2#sub-applications.

Here's the meaty part, in case the link dies:

To host an ASP.NET Core app as a sub-app under another ASP.NET Core app:

1.Establish an app pool for the sub-app. Set the .NET CLR Version to No Managed Code because the Core Common Language Runtime (CoreCLR) for .NET Core is booted to host the app in the worker process, not the desktop CLR (.NET CLR).

2.Add the root site in IIS Manager with the sub-app in a folder under the root site.

3.Right-click the sub-app folder in IIS Manager and select Convert to Application.

4.In the Add Application dialog, use the Select button for the Application Pool to assign the app pool that you created for the sub-app. Select OK.

As a bonus, my main application is legacy Web Forms and my sub-application is exciting new .NET Core functionality.

Solution 3

You don't need to create a site for each app, you can host it as before, as web applications inside one site. The only thing you need to do is select unmanaged app pool for your .Net Core applications, that's it. So this works: https://domain/app1 and https://domain/app2 and you only need 1 certificate for a web server

Alternatively, you can create site for each app, then you need a sub-domain for each app, like https://app1.domain and like https://app2.domain In this case all sub-domains must point to the same IP (if your server only have one) and you need to set up bindings for each site\app to it's domain. You should bind all sites to the same port (443 or 80).

For SSL in multiple sites configuration if you have IIS 7 you need to have a wildcard certificate. In IIS 8 and above you can use Server name indication feature and have a separate certificate for each site, however I recommend you to use wildcard certificate as well since SNI is new and might not be supported by some browsers

In both cases you need to set up a Windows Server Hosting from here Microsoft's download center

Solution 4

Suppose your main web directory is :

mainweb\

Make sure you have a valid ASP.NET application running in your App1 subfolder:

mainweb\App1\

Now install your dotnet core 2 or 3 app in the App1\ folder with all the dependencies etc.

Next, drop this web.config in that same folder:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\App1.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="outofprocess" />
    </system.webServer>
  </location>
</configuration>

Now suppose you want another dotnet core web api named App2\ running under mainweb also:

  1. Just create the new folder mainweb\App2\
  2. Create a new asp.net application in that folder via IIS or your special web hosting app.
  3. drop the web.config above and change the one line for the process path to .\App2.exe

When the mainweb ASP.NET application starts it will start the dotnet core apps also. All good and they run under your mainweb as :

https://mainweb.com/App1/WeatherForecast/ // WeatherForecast is controller name

https://mainweb.com/App2/<ApiControllerName>/

Solution 5

Try it: In file Web.config each application has a different name: <add name="herenewappname" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />

Share:
25,560

Related videos on Youtube

Rob McCabe
Author by

Rob McCabe

Software developer from Northern Ireland. I use technologies such as .net Core, Angular, Docker, Kubernetes and much more!

Updated on June 01, 2021

Comments

  • Rob McCabe
    Rob McCabe almost 3 years

    I am building a few web sites in ASP.NET Core (multiple user interface applications and a WebAPI app). They all work together, utilising the WebAPI. For the purpose of my question we'll call them App1, App2 and App3. I am hosting my websites in IIS.

    Typically, in the old .NET Framework (4.5.1), I would have one website in IIS, with multiple web apps to host my multiple applications. Like so:

    • WebSite1 - running on port 443
      • App1

      • App2

      • App3

    If the website (WebSite1) runs on port 443 (using the SSL cert), that means all apps are accessible via one domain url as follows:

    With different app names at the end of the URL to identify them.

    When hosting the new ASP.NET Core applications, I host in IIS as well, and following the documentation for ASP.NET Core web site deployment, I host each .NET Core web app in a different site within IIS.

    As I have one domain name for all my sites, for example, https://www.example.com (I'm using an SSL cert), I have to give the other websites different ports. So I end up with something like this:

    • WebSite1 - running on port 443

    • WebSite2 - running on port 444

    • WebSite3 - running on port 445

    All of these apps are then accessible via these domain name URLs:

    How do I host ASP.NET Core applications all under one website as different "Applications", meaning they resolve to the same domain name (https://www.example.com and the forward slash app name identifies which app we want)? OR alternatively, how do I take what I have configured now with the ports and route them all so App1 is default for example.com/, App 2 is example.com/app2/, and App 3 is example.com/app3/?

    Am I looking at this .NET Core deployment completely the wrong way?

    • Tseng
      Tseng about 7 years
      Did you tried WebListener on Windows or reverse proxy (IIS (windwos) or nginx (linux))?
    • Rob McCabe
      Rob McCabe about 7 years
      I didn't @Tseng - I've never actually used that before but I will run a quick google now!
  • Laserson
    Laserson almost 5 years
    I have multiple applications on my Ubuntu server and I'm using the same approach with Nginx that you described. But I can not solve an issue with application routing: when I make a request to /App1 it seems like this path is being processed by application behind of this location (for example 127.0.0.1:443/App1). But since there's no such route in the application it fails with error 404. Is there a solution for this problem?
  • nh43de
    nh43de over 3 years
    You can also use something like API Gateway if you're on Azure.
  • A Farmanbar
    A Farmanbar over 2 years
    I used your solution but not works :stackoverflow.com/q/69307858/6071729
  • Muzzy B.
    Muzzy B. about 2 years
    great! thank you, pal! that's what I was looking for!