开发者

MVC: Is it possible to override controller action?

开发者 https://www.devze.com 2023-03-03 02:31 出处:网络
A little background first as to why I need this. I am currently creating a CMS. If you imagine this CMS has a PageController which provides all the information a standard page needs, content, navigati

A little background first as to why I need this. I am currently creating a CMS. If you imagine this CMS has a PageController which provides all the information a standard page needs, content, navigation etc.

Now the CMS can be amended for each client using it, and should a client require extra/different information in their pages I would like to override the default PageController with one tailored specifically for their needs.

This is what I have tried:

Base controller

namespace CMS.Core.Controllers {
    public class PageController : Controller {
        public virtual ActionResult DisplayHome() {
            // Logic
            return View();
        }
    }
}

Client specific controller

namespace CMS.ClientCore.Controllers {
    public class PageController : Core.Controllers.PageController {
        public override ActionResult DisplayHome() {
            return Content("Client Home"); //开发者_运维知识库 Just for testing
        }
    }
}

Route

routes.MapRouteInLowercase(
    "Home",
    "",
    new { controller = "Page", action = "DisplayHome" },
    new[] { "CMS.Core.Controllers", "CMS.ClientCore.Controllers" }
);

The Error

The request for 'Page' has found the following matching controllers: PCCMS.Core.Controllers.PageController

PCCMS.ClientCore.Controllers.PageController

The cause of the error is obvious, so is there an alternative method to override a controller/controller action?


You are approaching the problem in the wrong way.

Create a IContentProvider which is used by the PageController, and let the content provider figure out what content the current customer needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消