【发布时间】:2014-01-05 23:14:04
【问题描述】:
我的应用程序有 MVC 控制器和 WebApi 控制器。到目前为止,我已经能够将控制器之间共享的任何逻辑分别推送到BaseController 和BaseApiController 中。
现在我遇到一种情况,API 控制器和 MVC 控制器都在做同样的事情——注册一个新的用户帐户。这个过程有点复杂,因为我有一个多租户应用程序,看起来像这样:
// MVCController : BaseController
public ActionResult Register(RegisterModel model) {
// see if the model is valid
// check and make sure the credentials check out with policy requirements
// add the user to the user table if they aren't already there
// create a tenancy
// assign cookie
}
大部分实际工作都被推到了服务层,但是调用和条件占用了大约 20 行代码,我不想将它们复制并粘贴到类似的 WebApi 控制器中。
所以我想知道我是否可以以某种方式让我的BaseController 和BaseApiController 从一个通用控制器继承,我可以在其中放置此身份验证代码。这里推荐的方法是什么?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 asp.net-web-api