How to create normal controllers and views in an ASP.NET MVC application that already has umbraco 8 installed

断了今生、忘了曾经 提交于 2019-12-24 11:17:13

问题


I would like to include normal controllers and views in my ASP.NET MVC application that already has Umbraco setup, pardon me if I am missing something as I am new to Umbraco.

I tried to follow this https://24days.in/umbraco-cms/2016/adding-umbraco-to-existing-site/ but its based on Umbraco 7 and I am unable to inherit from IApplicationEventHandler.

I have tried to add controller and views directly but the routing doesn't work, as Umbraco takeover the routing.

I would like to know how to create normal ASP.NET MVC controllers, views as well as their routing in Umbraco. TIA


回答1:


There is no IApplicationEventHandler in Umbraco8, they have replaced it with User Composers

Umbraco has its own global.asax implementation and as you said it overwrites the default routings. The usual routing class is not executed, you have to add your routings when the application starts.

I managed to do that with creating a User Composer. User composers compose after core composers, and before the final composer.

(Below, I create an IComposer, but IUserComposer should also work.)

public class ApplicationEventComposer : IComposer
{
    public void Compose(Composition composition)
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

In this one you can register your own RouteConfig, Bundles, etc. Just be careful, it is easy to mess up the Umbraco routings...

Here is an example to add a new controller called TestController:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            name: "Test",
            url: "Test/{action}/{id}",
            defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional }
        );
    }
}


来源:https://stackoverflow.com/questions/56817625/how-to-create-normal-controllers-and-views-in-an-asp-net-mvc-application-that-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!