asp.net-mvc asp.net mvc 把控制器放到一个单独的项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/401376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:15:12  来源:igfitidea点击:

asp.net mvc put controllers into a separate project

asp.net-mvc

提问by Aaron Palmer

I'm just learning asp.net mvc and I'm trying to figure out how to move my controllers into a separate project. Typically when I have designed asp.net web apps before, I created one project for my models, another for my logic, and then there was the web.

我只是在学习 asp.net mvc,我正试图弄清楚如何将我的控制器移动到一个单独的项目中。通常,当我之前设计 asp.net web 应用程序时,我为我的模型创建了一个项目,为我的逻辑创建了另一个项目,然后是 web。

Now that I'm learning asp.net mvc I was hoping to follow a similar pattern and put the models and controllers each into their own separate projects, and just leave the views/scripts/css in the web. The models part was easy, but what I don't understand is how to make my controllers in a separate project be "found". Also, I would like to know if this is advisable. Thanks!

现在我正在学习 asp.net mvc,我希望遵循类似的模式,将模型和控制器分别放入自己单独的项目中,并将视图/脚本/css 留在网络中。模型部分很简单,但我不明白的是如何“找到”单独项目中的控制器。另外,我想知道这是否可取。谢谢!

采纳答案by Craig Stuntz

First of all, it is certainly a good idea to put your model into a separate project. As you've discovered, this is trivial.

首先,把你的模型放到一个单独的项目中当然是个好主意。正如你所发现的,这是微不足道的。

Regarding Controllers and Views, I don't see any obvious advantage to separating them for most basic projects, although you may have a particular need to do so in a particular application.

关于控制器和视图,对于大多数基本项目,我认为将它们分开没有任何明显的优势,尽管您可能在特定应用程序中特别需要这样做。

If you do choose to do this, then you will need to tell the framework how to find your controllers. The basic way to do this is by supplying your own ControllerFactory. You can take a look at the source code for the DefaultControllerFactory to get an idea for how this is done. Subtyping this class and overriding the GetControllerType(string controllerName) method may be enough to accomplish what you're asking.

如果您选择这样做,那么您需要告诉框架如何找到您的控制器。执行此操作的基本方法是提供您自己的 ControllerFactory。您可以查看 DefaultControllerFactory 的源代码以了解这是如何完成的。对此类进行子类型化并覆盖 GetControllerType(string controllerName) 方法可能足以完成您的要求。

Once you've created your own custom ControllerFactory, you add the following line to Application_Start in global.asax to tell the framework where to find it:

创建自己的自定义 ControllerFactory 后,您可以将以下行添加到 global.asax 中的 Application_Start 以告诉框架在哪里可以找到它:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

Update:Read this postandthe posts it links to for more info. See also Phil Haack's comment on that post about:

更新:阅读这篇文章及其链接的文章以获取更多信息。另请参阅 Phil Haack 对该帖子的评论:

ControllerBuilder.Current.DefaultNamespaces.Add(
    "ExternalAssembly.Controllers");

...which is not a complete solution, but possibly good enough for simple cases.

...这不是一个完整的解决方案,但对于简单的情况可能已经足够了。

回答by ThisGuy

While it is reasonable to create your own ControllerFactory, I found it more convenient to define all my Controllers in each project, but derive them from Controllers in my Shared project:

虽然创建自己的 ControllerFactory 是合理的,但我发现在每个项目中定义我的所有控制器更方便,但从我的共享项目中的控制器派生它们:

namespace MyProject1.Controllers
{
   public class MyController : MySharedProject.Controllers.MyController
   {
      // nothing much to do here...
   }
}

namespace MySharedProject.Controllers
{
   public abstract class MyController : System.Web.Mvc.Controller
   {
      // all (or most) of my controller logic here...
   }
}

This has the added benefit that you have a place to put your Controller logic that differs from project to project. Also, it is easier for other developers to quickly find your Controller logic because the Controllers exist in the standard place.

这有一个额外的好处,即您可以放置​​不同项目的控制器逻辑。此外,其他开发人员更容易快速找到您的控制器逻辑,因为控制器存在于标准位置。

Regarding whether this is advisable, I think it absolutely is. I've created some common Account Management logic that I want to share between projects that otherwise have very different business logic. So I'm sharing my Account and Admin Controllers, but the other Controllers are specific to their respective projects.

关于这是否可取,我认为绝对是。我已经创建了一些通用的帐户管理逻辑,我想在具有非常不同的业务逻辑的项目之间共享这些逻辑。所以我共享我的帐户和管理控制器,但其他控制器特定于他们各自的项目。

回答by RandyMohan

  • Add the Class Library for your mvc project.
  • In the class add the following code(For u'r Controller Code)

    namespace ContactController
    {
    public class ContactController : Controller
    {
        public ActionResult Call()
        {
            ViewBag.Title = "Inside MyFirst Controller.";
            return View();
        }
    }
    

    }

  • On the mvc project view folder add the folder for Contact and create a Call.cshtml file. View Folder

  • Add the class library project reference into your main MVC project.

  • 为您的 mvc 项目添加类库。
  • 在类中添加以下代码(对于你的控制器代码)

    namespace ContactController
    {
    public class ContactController : Controller
    {
        public ActionResult Call()
        {
            ViewBag.Title = "Inside MyFirst Controller.";
            return View();
        }
    }
    

    }

  • 在 mvc 项目视图文件夹中添加 Contact 文件夹并创建 Call.cshtml 文件。 查看文件夹

  • 将类库项目引用添加到您的主 MVC 项目中。

Reference

参考

  • Finally to refer contact controller namespace into Route Config.
  • 最后将联系人控制器命名空间引用到路由配置中。

RouteConfig

路由配置

回答by Homayoun Behzadian

My problem solved after I updated System.Web.MvcNuGet reference so MvcWebsite and Class Library use same System.Web.Mvcversion

更新System.Web.MvcNuGet 参考后我的问题解决了,因此 MvcWebsite 和类库使用相同的System.Web.Mvc版本

No need to add default namespaces

无需添加默认命名空间

回答by Jamie

The simplest form of separation I use is to retain the Views "as is" in the original MVC project but remove the Controllers. Then in a new ClassLibrary project add the Controller classes and ensure they inherit from Controller.

我使用的最简单的分离形式是在原始 MVC 项目中“按原样”保留视图,但删除控制器。然后在一个新的 ClassLibrary 项目中添加 Controller 类并确保它们继承自 Controller。

The MVC routing engine will automatically route to the Controllers in the ClassLibrary and the Controllers will automatically construct the Views from the original MVC project, provided you have your references and usings correctly in place.

MVC 路由引擎将自动路由到 ClassLibrary 中的控制器,控制器将自动从原始 MVC 项目构建视图,前提是您的引用和使用正确到位。

I am using this architecture to implement an Html Reports module that can be compiled and deployed separately from the main solution. At last I am free from SSRS!

我正在使用这种架构来实现一个 Html 报告模块,该模块可以与主解决方案分开编译和部署。我终于摆脱了 SSRS!