Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Bundles returns 404

I have a project that works with bundling when you run it from within visual studio. However, after you do a deployment, the bundling handler never seems to pick up the route. It ends up going to the static file handler instead, which returns a 404 response.

Any ideas? I see the optimization assembly in the bin of the website under IIS.

It's using the 4.0 app pool and integrated mode.

I'm wondering if anyone has any ideas or suggestions?

Thanks

----- update based on questions -----

VS2012

targetFramework="4.5"

I also added some code into the view to show which modules were loaded and I can see the bundle module listed there.

BundleConfig is the default provided when using the Internet Application MVC4 project template.

The site is being deployed into the root. It's odd as when I set EnableOptimizations = true (due to running in debug mode via visual studio F5), it works perfect! I can navigate to content/css and it spits out the combined css.

I deploy it over and everything else works, but bundling!

like image 562
Mike Avatar asked Mar 13 '13 18:03

Mike


3 Answers

I've just hit (and solved) this problem.

Make sure your bundle's virtual path can't be confused for an existing directory or actual file name. In my case, I'd coded it as:

bundles.Add(new ScriptBundle("~/bundles/main.js").Include( ...

But when I changed it to

bundles.Add(new ScriptBundle("~/bundles/main").Include( ... 

it all started working.

like image 67
user2320070 Avatar answered Nov 16 '22 06:11

user2320070


Updated Answer on 11/17/2013 This is caused by the fact that the default MVC routing only handles * instead of * . *, i.e. IIS or IIS Express's applicationhost.config has the following:

            <add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />

So to workaround it, we can add the following to the web.config:

      <system.webServer>
        <handlers>      
          <add name="UrlRoutingHandler" 
               type="System.Web.Routing.UrlRoutingHandler, 
                     System.Web, Version=4.0.0.0, 
                     Culture=neutral, 
                     PublicKeyToken=b03f5f7f11d50a3a" 
               path="/bundles/*" 
               verb="GET"/>      
        </handlers>
      </system.webServer>

For more information, you can reference the following: http://weblogs.asp.net/owscott/archive/2013/01/16/handing-mvc-paths-with-dots-in-the-path.aspx ASP.NET MVC Url Route supporting (dot)

Old wrong answer: Basically, DOT is generally not allowed in virtual path when IIS parse URLs. This a Link mentioned the following URLScan AllowDotInPath parameter: By default, this option is set to 0. If this option is set to 0, URLScan rejects any request that contains multiple periods (.). This prevents attempts to disguise requests for dangerous file name extensions by putting a safe file name extension in the path information or query string portion of the URL. For example, if this option is set to 1, URLScan might permit a request for http:// servername/BadFile.exe/SafeFile.htm because it interprets it as a request for an HTML page, when it is actually a request for an executable (.exe) file with the name of an HTML page in the PATH_INFO area. When this option is set to 0, URLScan may also deny requests for directories that contain periods.

like image 25
xinqiu Avatar answered Nov 16 '22 04:11

xinqiu


Even I got the same error. Adding <modules runAllManagedModulesForAllRequests="true" /> under <system.webServer> in web.config file solved the problem.

like image 5
vineel Avatar answered Nov 16 '22 04:11

vineel