Orchard CMS : Javascript file returns 404 not found even though it exists
Asked Answered
A

2

6

I have the following in my Razor view for an editor template in my Orchard module:

Script.Include("assets.js").AtFoot();

When the page is rendered I can see this line at the bottom:

<script src="/Modules/MyModuleName/scripts/assets.js" type="text/javascript"></script>

Beautiful! Only problem is, when I visit that path I get a 404 error. The script doesn't exist.

...but it does! It's saved as Orchard.Web\Modules\MyModuleName\Scripts\assets.js

The rest of my module's functionality works fine - I can enable and use it, it just won't find the script file. Am I missing something obvious here?!

Autograft answered 15/3, 2012 at 13:16 Comment(4)
I assume the capitalization of /Scripts/ vs. /scripts/ is not the issue?Polemist
Hi @Alex, tried it with both :(Autograft
Do you have a web.config in your scripts folder with the right permissions?Footie
Ha! No! +1 for you, and @Footie for the answer :)Autograft
F
9

By default, Orchard is setup to restrict folder permissions. This is usually overriden by adding a web.config to each folder as required (in this case, your scripts folder).

If you use the codegen module to generate your module, then this is done for you as part of the generation. If not, then you need to add the web.config yourself.

The codegenned web.config looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>
  <system.web>
    <httpHandlers>
      <!-- iis6 - for any request in this location, return via managed static file handler -->
      <add path="*" verb="*" type="System.Web.StaticFileHandler" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>

    <handlers accessPolicy="Script,Read">
      <!--
      iis7 - for any request to a file exists on disk, return it via native http module.
      accessPolicy 'Script' is to allow for a managed 404 page.
      -->
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" preCondition="integratedMode" resourceType="File" requireAccess="Read" />
    </handlers>
  </system.webServer>
</configuration>
Footie answered 15/3, 2012 at 14:29 Comment(1)
Thanks - works now :) I originally created the module using the code gen tool but deleted the scripts folder. When I recreated it obviously the config file had gone. Doh!Autograft
D
2

I found an other reason for this 404 which I would like to mention. UrlScan by default rejects a dot in the path, I found this in my log: Rejected URL+contains+dot+in+path

So change the setting in:

AllowDotInPath=1

and it works again. It took me some time to find this, because I never use a dot in path...

Drabbet answered 13/3, 2013 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.