skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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" />
    

  2. 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’.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search