views:

91

answers:

2

Hi,

I need generate action links outside controllers.

I can use Html.Action in Views, Url.Action in Controllers. That's fine but...

I have my own class where I want generate HTML code for site menu, which is used in site.master. But how can I generate action links in my own class? Possible solution is to generate this HTML code in BaseController, but I don't want do this, because my class should be independent with ability to generate right links.

I've been googled for on hour, but didn't find solution.

Any help? Thanks

+1  A: 

Action Link generation depends on both the Routing configuration and the Controller Context. Both of these are only available inside the actual ASP.NET MVC pipeline. Just think of it: if I told you to generate an action link but told you nothing about the web application, how would you go about it?

If you are worried about SOLID, why not inject an UrlHelper, a HtmlHelper or the ControllerContext into your class that generates action links?

 void GenerateLinks(HtmlHelper html) { ... }
 void GenerateLinks(UrlHelper url) { ... }
 void GenerateLinks(ControllerContext context) { ... }
bzlm
+1  A: 

Pass yourself a reference to the Html object, so you can use it in your methods, when you instantiate the class. You can do it when you instantiate your menu object, or at each function call if they are static.

Palantir