ASP.NET MVC4部署至IIS(Win Server2016)后资源加载异常求助
Troubleshooting ASP.NET MVC4 Deployment Issues on Windows Server 2016 IIS
Hey there, let's work through these two common deployment issues step by step—they're totally fixable once you know where to look!
1. IIS Returns Directory Listing Instead of Serving JavaScript Files
This happens when IIS isn't configured to prioritize your application's routing or default documents over directory browsing. Try these fixes:
- Disable Directory Browsing: Open IIS Manager, select your site, go to the "Directory Browsing" feature in the middle pane, and click "Disable" in the right-hand actions menu. This prevents IIS from showing file lists when it can't find a default entry point.
- Verify Default Documents: Still in your site's settings, go to "Default Documents" and make sure
Default.aspx(the MVC fallback default) is added and positioned at the top. Alternatively, confirm your MVC routes are correctly set to route root requests toHome/Index. - Check ASP.NET 4.x Module Installation: Ensure the server has ASP.NET 4.6 (or matching version for MVC4) installed. Go to Server Manager > Add Roles and Features, navigate to Web Server (IIS) > Web Server > Application Development, and check "ASP.NET 4.6". Then verify
UrlRoutingModule-4.0exists in your site's "Modules" list—this is critical for MVC routing to work. - Confirm JS File Handler Mapping: In your site's "Handler Mappings", ensure
.jsfiles are mapped to theStaticFilehandler. If they're mapped to an ASP.NET handler by mistake, IIS will try to parse JS files as server-side code, leading to unexpected behavior.
2. Bundled CSS/JS Fail to Load with "Uncaught SyntaxError: Unexpected token <"
That error almost always means your browser is receiving HTML content (like a 404 error page or directory listing) instead of the expected bundled CSS/JS. Here's how to fix it:
- Enable Bundle Optimization: In your
Global.asaxfile, make sure you haveBundleTable.EnableOptimizations = true;set (usually insideApplication_Start()). Debug mode defaults this tofalse, but production environments need it enabled for IIS to properly serve bundled resources. - Install Static Content Feature: IIS needs the Static Content role to serve bundled files. Go to Server Manager > Add Roles and Features, select Web Server (IIS) > Web Server > Common HTTP Features, and check "Static Content". This lets IIS serve merged bundle files as static resources.
- Double-Check Bundle Paths: In your views, ensure you're using the correct path syntax for bundles, like
@Scripts.Render("~/bundles/jquery")and@Styles.Render("~/Content/css"). The~/prefix ensures the path resolves correctly relative to your site root—omitting it can lead to broken relative paths and 404s. - Verify Application Pool Permissions: The application pool identity for your site needs read access to the server's temporary cache directory where bundles are stored. Set the pool identity to
ApplicationPoolIdentity(recommended) and ensure it has read permissions onC:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files. - Check for Custom Bundle Config Issues: If you're using non-standard file extensions in your bundles or custom bundle transformations, make sure you've added the correct MIME types in IIS (e.g., for
.lessor.scssfiles) and that your bundle logic doesn't have typos in file paths.
内容的提问来源于stack exchange,提问作者tatunga23
相关产品推荐
相关产品推荐

