0

I'm trying to include js files that are not part of the original ~/Scripts folder included in a MVC web project. Rather I have my script files inside of my Feature folder like: ~/Features/MyNewFeature/Scripts

However when running the app I get nothing but 404 errors when the request goes to find those js files. I'm not sure what I'm missing or if there's something that needs to be added to make this work. I know I've read that js files have to be in the Scripts folder but I'm having a hard time believing that since there's a way around not having your cshtml files inside a /Views folder.

 bundles.Add(new ScriptBundle("~/bundles/mynewfeature")
                  .Include("~/Features/MyNewFeature/Scripts/Settings.js"));

Then to render in cshtml:

 @Scripts.Render("~/bundles/mynewfeature")
TMan
  • 4,044
  • 18
  • 63
  • 117
  • Does script file loads from features URL directly? I mean when you pass URL into browser – IT Man Jan 10 '18 at 21:22
  • @ITMan It does not. – TMan Jan 10 '18 at 21:26
  • You can put the scripts in any folder you want. –  Jan 10 '18 at 21:26
  • I seems there is someting wrong with your project settings, JavaScript files should be served as static content. – IT Man Jan 10 '18 at 21:32
  • Check this thread https://stackoverflow.com/questions/24763493/how-to-include-js-files-in-the-view-asp-net-mvc-4 – IT Man Jan 10 '18 at 21:34
  • Just tested this (created that folder structure and added a `Settings.js` to it) and it works fine. –  Jan 10 '18 at 21:41
  • @StephenMuecke did you need to add a script handler to the web.config? – TMan Jan 10 '18 at 22:02
  • No. But the fact you cannot navigate to it via the address bar suggests something else is wrong. Are you sure you do not have a typo? –  Jan 10 '18 at 22:04
  • @StephenMuecke I'm positive I don't have a typo. I'm going try and create a new mvc proj real quick to see if it works and if so maybe compare web.configs to see if something is missing. – TMan Jan 10 '18 at 22:13
  • @StephenMuecke Ok I narrowed it down to the web.config file. When the web.config file is moved from the Views folder into the Features folder, that is when the 404 errors starts. Any ideas? – TMan Jan 11 '18 at 00:11
  • Why do you need a web.config file in the features folder? –  Jan 11 '18 at 00:59
  • @StephenMuecke because if it doesn't I get a view error saying the view needs to have a WebViewer or WebViewer – TMan Jan 11 '18 at 04:13
  • @StephenMuecke http://davecallan.com/change-view-location-mvc-for-better-organisation/ – TMan Jan 11 '18 at 14:48
  • You hadn't mentioned that you also included views in the folder :) –  Jan 11 '18 at 20:30

1 Answers1

0

So the problem was first starting once the/Views Web.config got moved to the Features folder. To fix the problem I was able to add a new handler that explicitly allows js files.

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler" />
      <add name="JavaScript" path="*.js" verb="GET, HEAD" type="System.Web.StaticFileHandler" />
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
TMan
  • 4,044
  • 18
  • 63
  • 117