2

I have an existing Web Application hosted in IIS which an ASP.NET MVC application. Say this is hosted as www.mysite.com. I am trying to host a .NET Core 2.0 web application as a sub-application to this.

But when I try to access this as www.mysite.com/subaApp, IIS is looking for all my static files (js, css etc.) in the root path (www.mysite.com).

Please let me know if any solution is available for this. (My .Net Core 2.0 application is serving an Angular 5 app)

My default file (index.html) is generated as part of the Angular 5 build and it looks like this:

<script type="text/javascript" src="inline.318b50c57b4eba3d437b.bundle.js"></script>
<script type="text/javascript" src="polyfills.bf95165a1d5098766b92.bundle.js"></script>
<script type="text/javascript" src="main.e0d3a51586e8e9fd039d.bundle.js"></script>
Mojtaba
  • 2,764
  • 1
  • 21
  • 24
sunilbalan
  • 21
  • 3

3 Answers3

1

VS uses Angular CLI internally to build the angular files and deploy them. That's how your index file will be modified during the build process.

You can change the file package.json file in the client app folder with the content where your build command is set to the following manner

ng build --base-href  --deploy-url /subsite

This will change the path of the JS and css file paths which will be generated in the index.html file.

Reference

Naveen Kumar G C
  • 1,332
  • 1
  • 10
  • 12
0

In views processed by Razor, you can utilize "home-relative" URLs to ensure that the right URL (including your virtual directory) is placed. For example:

<script src="~/path/to/my/script.js"></script>

After Razor processes the view, that will end up looking like:

<script src="/subaApp/path/to/my/script.js"></script>

In regular C# code where you can either access an instance of UrlHelper (controllers, view components) or inject IUrlHelperFactory and create one yourself (other classes that operate within the request lifecycle, such as tag helpers), you can utilize the Content method to get that contextual URL.

For references in other static files, such as CSS imports, CSS background images, etc. you should use path-relative URLs. For example, if you need to include a background image in wwwroot\images in your stylesheet in wwwroot\css, then you can just do:

background-image: url('../images/myimage.jpg');

In other words, go up one level from current directory (wwwroot\css to wwwroot) then into the images directory (wwwroot\images).

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • As I mentioned it is basically an Angular 5 app so I am directly using the index.html which is getting generated when I build the angular app.I t has all the paths defined when it is generated. This is working fine when I deploy this as the root app.But problems arise when it is deployed as sub application. So there should be some way to tell the IIS that my root is not '/" but '/subApp. – sunilbalan Mar 09 '18 at 02:39
  • This isn't behavior controlled by Angular, it is how the browser is interpreting the path (or lack of path you provided). You need to prefix your `src` path to the script with `~/`. – Marc LaFleur Mar 09 '18 at 14:07
  • Take a look at this answer for a great explanation of how browsers interpret paths: https://stackoverflow.com/a/24028813/105518 – Marc LaFleur Mar 09 '18 at 14:11
  • @chrispratt can you guide us if angular app has been selected as template. Their will be no razor views for this. – Naveen Kumar G C Nov 05 '18 at 10:12
0

You should set the root directory when the Web host is built in the Program.cs file in the .NET Core application. Use the below method to set the content root.

UseContentRoot(Directory.GetCurrentDirectory()

Your Program.cs file should look more similar to this.

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
}