You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在已安装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):

1. Create Your Regular MVC Controller

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 Controllers folder, select Add → MVC Controller - Empty, and name it something like CustomPageController
  • 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();
    }
}
2. Add Matching Views

Stick to the standard MVC view structure so the framework can find your views automatically:

  • Create a CustomPage folder inside your Views directory
  • 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>
3. Configure Routing to Bypass Umbraco

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 Composers folder in your project so Umbraco automatically discovers it. If not, you may need to register it manually in your web.config.

4. Test Your Routes

You should now be able to access your regular MVC pages with these URLs:

  • https://yourdomain.com/custom → Triggers CustomPageController.Index()
  • https://yourdomain.com/custom/details/123 → Triggers CustomPageController.Details(123)
Quick Troubleshooting Tips
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:37:14