Orchard CMS : Javascript file returns 404 not foun

2019-04-28 05:45发布

问题:

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?!

回答1:

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>


回答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...