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.

2
  • Does this answer your question? How to host multiple .NET Core apps under the same URL? Commented Oct 18, 2021 at 9: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 <url>/css instead of <url>/config/css. Commented Oct 18, 2021 at 10:32

2 Answers 2

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" />
Sign up to request clarification or add additional context in comments.

Comments

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'.

3 Comments

I can't seem to get it to work. Adding <base href="http://host/setup"> 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.
Try adding a slash (/) in front: href="/favicon.ico"
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.