1

I have two ASP .Core MVC applications that are hosted under the same url.
I've managed to separate them with Nginx so that a certain path goes to app-2, while the rest goes to app-1:
http://host -> app-1
http://host/setup -> app-2

My problem comes when the user connects to app-2, as the application still thinks it's app-root is http://host.
This leads to the client encountering a 404 when for example style sheets are downloaded, since app-2.css exists under http://host/setup/css but the application searches in http://host/css.

The "include"-lines in the .cshtml file in app-2 look like this:

<link rel="stylesheet" type="text/css" href="@Url.Content("~/css/app-2.css")" asp-append-version="true" />

Is there some way of "overriding" or telling app-2 that ~ should refer to <host>/setup/css/ instead of <host>/css/?
I really don't want to hardcode it in case the url changes at some point.

Jabbl
  • 59
  • 7
  • Does this answer your question? [How to host multiple .NET Core apps under the same URL?](https://stackoverflow.com/questions/42348511/how-to-host-multiple-net-core-apps-under-the-same-url) – JHBonarius Oct 18 '21 at 09:21
  • Not quite; my routing between the web apps works, but they both think their webroot is the same. I am able to successfully connect to `app-2`, but it fails to download `app-2.css` since it believes it exists under `/css` instead of `/config/css`. – Jabbl Oct 18 '21 at 10:32

2 Answers2

0

After many hours of searching I found no way of altering the application root for the entire webserver.
What I ended up doing instead was create class PathHelper with options and added it to Startup.cs:

class PathHelper
{
    public PathHelper(IOptions<PathHelperOptions> opt)
    {
        Path = opt.Path;
        
        if (Path.StartsWith('/'))
        {
            Path = Path[1..];
        }
        if (!Path.EndsWith('/'))
        {
            Path = Path + '/';
        }
    }

    public string Path { get; }
}

class PathHelperOptions
{
    public string Path { get; set; }
}

# Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  services
      .AddScoped<PathHelper>()
      .Configure<PathHelperOptions>(opt =>
      {
          opt.Path = this.configuration.GetSection("URL_SUFFIX");
      });

  [...]
}

I then used it in the .cshtml files like this:

@inject PathHelper helper
<link rel="stylesheet" type="text/css" href="@Url.Content(helper.Path + "css/app-2.css")" asp-append-version="true" />
Jabbl
  • 59
  • 7
0

I think the easiest way is to include the base tag in pages coming from 'app-2'.

Try it like this:

<html>
    <head>
        <base href="http://host/setup">
    </head>

Now your relative links are sent to 'app-2'.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • I can't seem to get it to work. Adding `` to the top of the header still results in the files being loaded from `host`. Example: favicon with `href="favicon.ico"` is loaded from `http://host/favicon.ico`. – Jabbl Oct 20 '21 at 05:46
  • Try adding a slash (`/`) in front: `href="/favicon.ico"` – Poul Bak Oct 20 '21 at 06:34
  • Hmm, no. I've also tested hardcoding `base` to `http://localhost/setup` and setting favicon to `href="/favicon.ico"`, but the icon is still loaded from `localhost/favicon.ico`. – Jabbl Oct 21 '21 at 06:47