发布自定义MVC项目后Kentico的Global.asax文件被覆盖问题
Hey there, I’ve dealt with this exact headache before when merging custom MVC code with a Kentico site—nothing’s more frustrating than overwriting that global.asax and watching core Kentico functionality break! Let’s walk through the best ways to fix this:
Kentico’s default global.asax inherits from CMSApplication, which handles all the critical initialization logic for the platform. Instead of using the default HttpApplication for your MVC project, inherit from CMSApplication and make sure to call the base class’s initialization first before adding your custom code:
public class MvcApplication : CMSApplication { protected void Application_Start() { // Run Kentico's core initialization first base.Application_Start(); // Now register your custom MVC routes, filters, etc. RouteConfig.RegisterRoutes(RouteTable.Routes); // Add any other custom startup logic here } }
This way, you’re not replacing Kentico’s global logic—you’re extending it. Just make sure your project references the necessary Kentico assemblies (like CMS.Core.dll and CMS.Base.dll) to access CMSApplication.
Kentico is designed to be extended via modules, not direct global.asax modifications. This approach avoids any overwrite issues entirely, since your custom code hooks into Kentico’s lifecycle without touching the core global.asax.
Here’s how to set it up:
- Create a custom module class in your MVC project:
[assembly: RegisterModule(typeof(CustomMvcRouteModule))] public class CustomMvcRouteModule : Module { public CustomMvcRouteModule() : base("CustomMvcRouteModule") { } protected override void OnInit() { base.OnInit(); // Hook into Kentico's pre-initialization event to register routes ApplicationEvents.PreInit.Execute += RegisterCustomRoutes; } private void RegisterCustomRoutes(object sender, EventArgs e) { // Register your MVC routes here RouteConfig.RegisterRoutes(RouteTable.Routes); } }
- Build and deploy your MVC project to the Kentico site. Kentico will automatically detect and load your module during startup, executing your route registration at the right time without interfering with core functionality.
This method is future-proof too—if you upgrade Kentico later, you won’t have to re-sync global.asax code.
If you absolutely need to keep your own standalone global.asax, you can manually merge Kentico’s core logic into it. Note: This is not recommended long-term, as Kentico updates may change the core global.asax code, requiring you to re-merge every time.
Copy all the event handlers from Kentico’s original global.asax into your custom one, then add your MVC code:
public class MvcApplication : HttpApplication { protected void Application_Start() { // Copy Kentico's core initialization CMSApplication.Init(); // Add your custom MVC setup RouteConfig.RegisterRoutes(RouteTable.Routes); } // Copy all other Kentico event handlers protected void Application_BeginRequest() { CMSApplication.BeginRequest(this); } protected void Application_EndRequest() { CMSApplication.EndRequest(this); } protected void Application_Error() { CMSApplication.Error(this); } // Add any other required event handlers from Kentico's global.asax }
Quick Deployment Tip
When publishing your custom MVC project to the Kentico IIS site, make sure to exclude the global.asax file from your publish settings (if you’re using the inheritance or module approach) to avoid accidentally overwriting Kentico’s version.
内容的提问来源于stack exchange,提问作者Ali

