Search code examples
angularasp.net-coreangular7

Why are javascript files from angular project not being identified as javascript?


I have been working integrating an Angular 7 project into a ASP.NET Core MVC project but have recently run into an issue. When testing the solution, I ran into a problem in which Edge gave a HTTP 404 error. When tested on Firefox, the Javascript console gave a warning messaged that read Javascript files are being identified as HTML files. The only actions I have taken are to build the Angular project, copy the Angular project into a subfolder in the ASP.NET Core project, then adding the built files to a razor file. The razor file:

@{
    ViewData["Title"] = "Home Page";
}

<h1>This is the Index page</h1>
@section Scripts{
    <script src="~/ClientApp/dist/frontend/runtime.js"></script>
    <script src="~/ClientApp/dist/frontend/polyfills.js"></script>
    <script src="~/ClientApp/dist/frontend/styles.js"></script>
    <script src="~/ClientApp/dist/frontend/vendor.js"></script>
    <script src="~/ClientApp/dist/frontend/main.js"></script>
}

Can someone explain where I am going wrong?


Solution

  • If you would like to have an access to a folder outside the wwwroot, assume ClientApp foler ,you need to configure app.UseStaticFiles() in startup:

    public void Configure(IApplicationBuilder app)
    {
    app.UseStaticFiles(); // For the wwwroot folder
    
    app.UseFileServer(new FileServerOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "ClientApp")),
        RequestPath = "/ClientApp",
        EnableDirectoryBrowsing = true
    });
    }
    

    Refer to https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#usefileserver