1

I am trying to add AngularJS in my mvc project.

This is my bundle config

  public static void RegisterBundles(BundleCollection bundles) {

           bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/angularjs").Include(
                       "~/Scripts/angular.js",
                        "~/Scripts/angular-ui-router.js"));

               bundles.Add(new ScriptBundle("~/bundles/appscripts").Include(
                   "~/Areas/app.module.js",
                   "~/Areas/app.component.js",
                   "~/Areas/app.routes.js",

     bundles.Add(new ScriptBundle("~/bundles/appcomponents").Include(
      "~/Areas/Home/Views/Default/default.component.js"));
        }

_Layout.cshtml

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/angularjs")
    @Scripts.Render("~/bundles/appscripts")
    @Scripts.Render("~/bundles/appcomponents")
    @RenderSection("scripts", required: false)

I am able to add ~/bundles/jquery, ~/bundles/angularjs and ~/bundles/appscripts

enter image description here

but I am unable to add ~/bundles/appcomponents and I am getting a 404 error for this script.

enter image description here

I also tried adding the script file directly in the _layout.cshtml

<script src="~/Areas/Home/Views/Default/default.component.js"></script>

and I am getting the same error.

I am not sure why I am not able to add any scripts from the Views folder.

Can anyone please let me know how I can add this script file in the bundle config?

Win
  • 61,100
  • 13
  • 102
  • 181
Pein
  • 423
  • 3
  • 11
  • 27
  • Please share a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ele Feb 22 '18 at 20:28
  • Can you add in what is in your `RouteConfig`? – Rhumborl Feb 22 '18 at 20:36
  • public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } – Pein Feb 22 '18 at 21:29
  • @YogendharReddy use the [edit] button, located under the tag list, to add that to your question. Comments aren't really intended for large blocks of code. – Tieson T. Feb 22 '18 at 22:43

1 Answers1

3

Due to security reason, web.config inside View folder blocks accessing files from browser. However, you can configure it to allow -

<?xml version="1.0"?>

<configuration>
  <configSections>
  ...        
  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*.js" verb="*" 
          preCondition="integratedMode" 
          type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
   ...
</configuration>

enter image description here

Here is my answer similar to this question, but it is about cshtml file.

Win
  • 61,100
  • 13
  • 102
  • 181