How to register a Controller into ASP.NET MVC when the controller class is in a different assembly?

前端 未结 4 2031
误落风尘
误落风尘 2020-12-13 02:20

My goal is to modify asp.net mvc\'s controller registery so that I can create controllers and views in a separate (child) assembly, and just copy the View files and the DLLs

相关标签:
4条回答
  • 2020-12-13 02:58

    Create a separate module as shown Plugin Project structure:

    Notice that some files where excluded from the project.

    The MessagingController.cs file is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MessagingModule.Controllers
    {
        public class MessagingController : Controller
        {
            //
            // GET: /Messaging/
    
            public ActionResult Index()
            {
              var appName = Session["appName"]; // Get some value from another module
                return Content("Yep Messaging module @"+ appName);
            }
    
        }
    }
    

    The MessagingAreaRegistration file is as shown:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    
    namespace MessagingModule
    {
      public class MessagingAreaRegistration : AreaRegistration
      {
        public override string AreaName
        {
          get
          {
            return "Messaging";
          }
        }
    
        public override void RegisterArea( AreaRegistrationContext context )
        {
          context.MapRoute(
              "Messaging_default",
              "Messaging/{controller}/{action}/{id}",
              new { action = "Index", id = UrlParameter.Optional }
            //new[] { "MessagingModule.Controllers" }
          );
        }
    
      }
    }
    

    The relevant potion of the Global.asax.cs file is as follows:

    [Load the external plugin (controllers, api controllers library) into the current app domain. After this asp.net mvc does the rest for you (area registration and controllers discovery ][3]

       using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Web;
       using System.Web.Http;
       using System.Web.Mvc;
       using System.Web.Optimization;
       using System.Web.Routing;
       using System.Reflection;
       using System.Web.Configuration;
       using System.Text.RegularExpressions;
    
       namespace mvcapp
       {
         // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
         // visit http://go.microsoft.com/?LinkId=9394801
    
         public class MvcApplication : System.Web.HttpApplication
         {
           protected void Application_Start()
           {
             var domain = AppDomain.CurrentDomain;
    
             domain.Load("MessagingModule, Version=1.0.0.0, Culture=neutral,   PublicKeyToken=null");
    
    0 讨论(0)
  • 2020-12-13 03:13

    my experience with controllers is that this just works. Copy the assembly into the bin directory (or add a reference to the assembly and it will be copied there for you)

    I have not tried with views

    0 讨论(0)
  • 2020-12-13 03:17

    Your other project should be set up like an area and you will get the desired result. The area registration class will bring your area project into the mix. Then you can just drop the dll into a running app and it will run without building the entire app and redeploying it.

    The easiest way to do it is to add a new mvc project to your solution and have Visual Studio create the new project inside /areas/mynewprog/. Delete out all the files you don't need and add an area registration class to wire it up.

    Build the project, grab it's dll and drop it into your running websites bin directory and that its..

    You then just have to deal with the views and stuff, either compile them into the dll or copy them to the server into the areas/mynewproj/ folder.

    0 讨论(0)
  • 2020-12-13 03:20

    Reference the other assembly from the 'root' ASP.NET MVC project. If the controllers are in another namespace, you'll need to modify the routes in global.asax, like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { typeof(HomeController).Namespace }
        );
    
    }
    

    Notice the additional namespaces argument to the MapRoute method.

    0 讨论(0)
提交回复
热议问题