sunilbalan
sunilbalan

Reputation: 21

ASP.net Core 2.0 Application as Sub Application

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>

Upvotes: 2

Views: 1194

Answers (3)

Asela Liyanage
Asela Liyanage

Reputation: 106

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();
}

Upvotes: 0

Naveen Kumar G C
Naveen Kumar G C

Reputation: 1392

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

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239440

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

Upvotes: 0

Related Questions