I have an ASP.NET MVC3 (with Razor) application where I allow users to specify components for the view -- like the layout, or partials that I use to render content. (For example, I have a model that has a Title
and Description
, and I allow users to specify a CSHTML partial in a particular directory that I use to render the data.)
The problem that I'm running into is isolation and ease of use for my users. For example, they can currently write code in their partials like:
@Html.ActionLink("edit", "Edit", "Content", new { Id = Model.Id }, null)
I would instead like them to write something like:
@SomeHelper.EditLinkFor(Model.Id)
Right now, my controllers are all part of the public API. Users need to know controller and action names to specify certain links (eg. edit link). I would like to provide a simplified API.
But how do I do it? If I just create the SomeHelper
class, trying to call @Html.ActionLink
isn't making much progress. I tried writing:
return System.Web.Mvc.Html.LinkExtensions.ActionLink(null, content.Title, "Details", "Content", new { Id = content.Id }, null);
But I get a null pointer exception. Creating an HtmlHelper
instance for the first parameter is not easy.
What are my options? Am I stuck with providing them full access to all my controllers as part of my API? Is there no way to provide a simplified API for their consumption? (I'm not trying to block them from writing Razor code; only encapsulate and simplify calls, and eliminate an underlying dependency on my controllers and models.)