0

I want to use method Html.Action() in my project. I have two projects.

  • project 1 - area - HomeController - IndexAction
  • project 2 - i write a function helper to use in my layout.

    public static IHtmlString RenderTest(this HtmlHelper htmlHelper)
    {
        string mhs = "";
        mhs += htmlHelper.Action("Index", "Home", new { area = "area" });
        return new MvcHtmlString(mhs);
    }
    

for project 1, I write a route map:

context.MapRoute("area_default",
    "Theme/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { string.Format("{0}.Controllers", this.GetType().Namespace) }
);

How can I use this function to load a controller that is in another assembly?

Html.Action("Index","Home", new { area = "area" });


In addition I have a duplicate controller names in each assembly,
e.g. Namespace1.FooController and Namespace2.FooController

I don't have a problem with my routes. Also, I can call any controller in different assemblies via URL/routes.
But I cann't use these urls in my HtmlHelper.Action().
Actually I want to call an action in controller that is in another assembly and get the view of that action as HtmlString but processed.

Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
Reza A.b.
  • 11
  • 3

1 Answers1

0

If you are sure you don't have duplicate controller names in each assembly, e.g. Namespace1.FooController and Namespace2.FooController, then you can simply just add all the namespaces that should be searched in the array for the namespaces param.

However, if you do have duplicate names, then the route will end up matching multiple controllers, which is a problem. There's no order of ops to the namespaces parameter - all are searched and all are treated equally, despite the order of the namespaces. If that's the case, then you'll have to define multiple routes, each tied to a specific namespace.

UPDATE

actually i want call a action in controller in another assembly and get the view of that action as HtmlString but processed.

Oh. You mean you literally want to call the action like a method in another piece of code, rather than get to it via a URL? You could use something like WebClient to actually make a request and get the response as a string. That's admittedly probably not the most efficient way, but it likely would be the easiest.

The alternative is much more complicated. If it's just a regular action that returns a ViewResult, then that's what you'll actually get back from calling it like a method. However, from a quick debugging session, it appears that the ViewResult is not processed until after it's returned from the action and goes back into pipeline (not just another action). As a result, all you get is an object with the name of the view to be used and all the view data that should be used to render it with, i.e. not an actual rendered string that you could do anything with. However, using something like RazorEngine, you might be able to manually take the data from the ViewResult and do something with it.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I don't have a problem with my routes. and i can call any controllers in different assemblies by URL and my routes. But I cann't use these urls in my `helper.Action()`. In addition i have a duplicate controllers like your example. – Reza A.b. Oct 06 '15 at 15:37
  • actually i want call a action in controller in another assembly and get the view of that action as HtmlString but processed. – Reza A.b. Oct 06 '15 at 15:47