0

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.)

ashes999
  • 9,925
  • 16
  • 73
  • 124

2 Answers2

1

The html helpers provided are pretty basic - but you certainly can create a helper. Just be careful - the existing API is provided and fits in exactly with the rest of the MVC programming model (Ajax and html helpers and the route syntax)

The problem is in how you call the code. Please post all of your code so we can check it out.

The question is exactly what do you want to do? You want to create a helper for EditLinkFor - but something is failing, can we see all the code?

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • My question is not so much about specific code that's failing; my question is more about the overall design. I want to provide simplified helpers, instead of requiring users to use (and therefore, know) my existing controllers and views. Maybe I'm barking up the wrong tree? – ashes999 May 24 '11 at 20:18
  • Theres nothing that says you can't. Without seeing how complex your routes would be otherwise and how inexperienced the users would be it is hard to say. However, a controller has a specific purpose. If you name it a basic way and provide documentation, a developer should be able to easily use it successfully. I don't know the skill level of people - but if theres basic documentation around your controllers/methods then at least it is consistent in the app with the rest of MVC. – Adam Tuliper May 25 '11 at 13:27
1

There are a few possible options:

a) You can extend the HtmlHelper with your own class

b) Reusing @helpers accross multiple views

c) Change the base type of your views

Community
  • 1
  • 1
Kalman Speier
  • 1,937
  • 13
  • 18
  • Option a is cool, but not what I need; option b essentially was the answer, albeit I had to crawl a long way down a rabbit hole to get it to work. – ashes999 May 25 '11 at 14:40
  • I have to again ask though is it necessary to go against the smooth simple controller/action syntax of MVC? Are the users that inexperienced? Controller/Action syntax makes it easy and anyone who knows MVC can easily find what code to follow. – Adam Tuliper May 25 '11 at 14:56