在已安装Umbraco 8的ASP.NET MVC应用中创建常规控制器与视图
Hey there! Let's get your regular ASP.NET MVC controllers and views working alongside Umbraco without those frustrating routing issues. Since the Umbraco 7 tutorial you tried is outdated (RIP IApplicationEventHandler), here's a modern, step-by-step guide tailored for newer Umbraco versions (8 and above):
No need to inherit any Umbraco-specific base classes here—just build a standard MVC controller like you normally would:
- Right-click your project's
Controllersfolder, select Add → MVC Controller - Empty, and name it something likeCustomPageController - Add your action methods as you would in a non-Umbraco MVC app:
public class CustomPageController : Controller { public ActionResult Index() { return View(); } public ActionResult Details(int id) { ViewBag.ItemId = id; return View(); } }
Stick to the standard MVC view structure so the framework can find your views automatically:
- Create a
CustomPagefolder inside yourViewsdirectory - Add views for each action method (e.g.,
Index.cshtml,Details.cshtml) with regular Razor markup:
@{ ViewBag.Title = "My Custom MVC Page"; } <h1>This is a regular MVC page—Umbraco won't touch it!</h1> <p>Item ID from URL: @ViewBag.ItemId</p>
The core problem is Umbraco's routing system taking over all requests. We need to tell it to ignore your custom controller routes. Here's how to do it based on your Umbraco version:
For Umbraco 9+ (.NET Core/.NET 5+)
In your Program.cs file, register your custom routes before setting up Umbraco's middleware, then exclude those paths from Umbraco's routing:
// First, register your custom MVC route app.MapControllerRoute( name: "CustomPageRoute", pattern: "custom/{action=Index}/{id?}", defaults: new { controller = "CustomPage" }); // Then set up Umbraco, and tell it to ignore your custom path app.UseUmbraco() .WithMiddleware(u => { u.UseBackOffice(); u.UseWebsite(); }) .WithEndpoints(u => { u.UseInstallerEndpoints(); u.UseBackOfficeEndpoints(); u.UseWebsiteEndpoints(); // Exclude all paths starting with "custom/" from Umbraco's routing u.WebsiteEndpointsOptions().RouteCollection.IgnoreRoute("custom/{*pathInfo}"); });
For Umbraco 8 (.NET Framework)
Create a composer and component to modify Umbraco's route ignore rules:
using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Web.Mvc; public class CustomRouteComposer : IComposer { public void Compose(Composition composition) { composition.Components().Append<CustomRouteComponent>(); } } public class CustomRouteComponent : IComponent { public void Initialize() { // Ignore any requests starting with "custom/" RouteTable.Routes.IgnoreRoute("custom/{*pathInfo}"); } public void Terminate() { } }
Pro tip: Put this code in a
Composersfolder in your project so Umbraco automatically discovers it. If not, you may need to register it manually in yourweb.config.
You should now be able to access your regular MVC pages with these URLs:
https://yourdomain.com/custom→ TriggersCustomPageController.Index()https://yourdomain.com/custom/details/123→ TriggersCustomPageController.Details(123)
- Order matters! Always register your custom routes before Umbraco's routing middleware/endpoints—otherwise Umbraco will still catch the requests.
- Double-check your view folder structure: it should be
Views/[ControllerName]/[ActionName].cshtml - If changes don't show up, clear Umbraco's cache via the backoffice: Settings → Cache → Clear All Cache
内容的提问来源于stack exchange,提问作者user167124

