生产环境下ASP.Net MVC路由URL返回404,是否需额外配置?
Hey there! It sounds like your static content is serving just fine (since index.html loads without issue), but the MVC routing system isn't kicking in for your controller/action requests. Let's walk through the most common fixes to get your app up and running:
Check IIS Modules & ASP.NET Installation
MVC relies on specific ASP.NET modules to handle route-based requests. For .NET Framework apps, make sure the ASP.NET 4.x feature is installed on your server (you can set this up via Server Manager > Add Roles and Features). For ASP.NET Core apps, you need the ASP.NET Core Hosting Bundle installed—it adds the necessary modules to route requests to your Core application.
In IIS Manager, navigate to your site > Modules and verify routing-related modules are present (likeUrlRoutingModule-4.0for .NET Framework orAspNetCoreModuleV2for ASP.NET Core).Validate Routing Configuration
Double-check your route setup to ensure it's mapping requests correctly:- For .NET Framework: Open
Global.asaxand confirm the default route is registered inRouteConfig.cs:
Also make sure your controller class names end withroutes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );Controller(e.g.,HomeController) and your action method is public. - For ASP.NET Core: Check
Program.cs(orStartup.csfor older versions) to ensureMapControllerRouteis configured properly for your routes.
- For .NET Framework: Open
Enable the URL Rewrite Module
IIS needs the URL Rewrite module to translate cleancontroller/actionURLs into requests the MVC framework can process. If this module isn't installed, IIS will treat your route as a missing static file and return a 404. You can install it through the IIS Manager's Web Platform Installer.Verify Application Pool Settings
Your app pool's configuration must match your MVC project's framework:- For .NET Framework: Set the .NET CLR Version to
v4.0(matching your project's target framework) and confirm the pool is running. - For ASP.NET Core: Set the .NET CLR Version to
No Managed Codesince Core apps self-host their runtime.
Also, ensure the app pool identity has read permissions to your application's root directory.
- For .NET Framework: Set the .NET CLR Version to
Check Web.config for Routing & Error Settings
- For .NET Framework: Ensure your
Web.configincludes the routing module registration. You can temporarily set<customErrors mode="Off"/>to see detailed error messages instead of the generic 404—this might reveal missing DLLs or configuration missteps. - For ASP.NET Core: Confirm the auto-generated
web.confighas the correctAspNetCorehandler configuration pointing to your app's DLL.
- For .NET Framework: Ensure your
Confirm Deployment Files Are Complete
Double-check that all necessary files are deployed:- For .NET Framework: Make sure the
bindirectory contains all required DLLs (likeSystem.Web.Mvc.dll,System.Web.Routing.dll, and your project's compiled DLLs). Missing these will break routing entirely. - For ASP.NET Core: Verify all published files (including runtime files, your app DLL, and configuration files) are present on the server.
- For .NET Framework: Make sure the
Start with checking IIS modules and app pool settings first—those are the most common culprits when static files work but MVC routes don't. Enabling detailed errors will also give you a clearer picture of exactly what's going wrong!
内容的提问来源于stack exchange,提问作者Imad

