开发者

Asp.net MVC RenderAction method in multiple controller

I am doing some plugin kind of work using asp.net mvc. I have two plugins(mvc projects) Say users and home. In both the plugins, I have Home Controllers. Yes the names are home controller in both the plugins(projects). When I build the project the Dlls are copied to the host project (third one).

this is home plugin

namespace Plugin.Home.Controllers
{
public class HomeController : Controller
{
    public string Index()
    {
        return "Home from home";
    }

    public string JustATest()
    {
        return "Just a test from home";
    }

}
}

Here is another controller in different project(User Plugin)

this is home controller

namespace Plugin.Users.Controllers
{
public class HomeController : Controller
{
    public string Index()
    {
        return "Ho开发者_运维百科me from Users";
    }

    public string JustATest()
    {
        return "Just a test from Users";
    }

    public string JustAnotherTest()
    {
        return "Just another test from Users";
    }

}
}

In global.asax I have register routes using namespaces.

routes.MapRoute(
        "Default", // Route name
        "Home/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },     
 new[] { "Plugin.Home.Controllers" }

routes.MapRoute(
           "Users", // Route name
           "users/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "Index", id = UrlParameter.Optional },     new[] { "Plugin.Users.Controllers" }
       );

I can access all the routes in the browser without any problem.

Now the problems

  1. I try to use @{Html.RenderAction("JustATest","Home");} It only renders from the home controller in home module. How can i render it from Users module.

  2. It throws an exception saying method not found in home controller, when I say @{Html.RenderAction("JustAnotherTest","Home");}

At what stage I can check if a given controller has the given method or not (Some ControllerFactory). Or how can i make sure it picks up the right controller.

Help will be appreciated.

Regards

Parminder


If you really need to save controller names both HomeController, i see simple solution to use hack like this:

routes.MapRoute(
        null,
        "{home}/{action}/{id}", 
        new { controller = "Home", action = "Index", home="Home", id = UrlParameter.Optional });
routes.MapRoute(
        null,
        "{home}/{action}/{id}", 
        new { controller = "Home", action = "Index", home="Users", id = UrlParameter.Optional }
       );

And you can access to this actions both by url and with RenderAction:

@{Html.RenderAction("JustATest","Home", new {home="Home"});}
@{Html.RenderAction("JustATest","Home", new home="Users");}

But I think that problem is artificial (because it is more hard that you imagine), and you use means only for using means, but not for solving real problem. For real plugin architecture you need to create at least: 1. Custom Route Constraint for checking, that controller type is from assembly, where controller was defined. 2. Installer, that will install all routes independently of main application 3. Create unit tests for each plugin application Routes to ensure that all routes, that was installed from plugins, that do not know anything about each other, works properly (and don't break each other).

I recommend to take your controllers at main web application and give them different names.


See the format used here:

render action with lambda

@Html.RenderACtion<Plugin.Home.Controllers.HomeController>(...)

Edit

I see your issue now. It's that they are dynamically loaded at runtime and hence the names aren't known when you compile. Check out using DefaultControllerFactory as mentioned in various postings here:

How to achieve a dynamic controller and action method in ASP.NET MVC?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜