Razor 和为用户创建助手:Html.*
我有一个 ASP.NET MVC3(带有 Razor)应用程序,允许用户指定视图的组件,例如布局或我用来呈现内容的部分。 (例如,我有一个具有 Title
和 Description
的模型,并且我允许用户在我用来呈现数据的特定目录中指定 CSHTML 部分。 )
我遇到的问题是用户的隔离和易用性。例如,他们目前可以在其部分代码中编写如下代码:
@Html.ActionLink("edit", "Edit", "Content", new { Id = Model.Id }, null)
我希望他们编写如下代码:
@SomeHelper.EditLinkFor(Model.Id)
现在,我的控制器都是公共 API 的一部分。 用户需要了解控制器和操作用于指定某些链接的名称(例如编辑链接)。我想提供一个简化的API。
但我该怎么做呢?如果我只是创建 SomeHelper
类,尝试调用 @Html.ActionLink
不会取得太大进展。我尝试写:
return System.Web.Mvc.Html.LinkExtensions.ActionLink(null, content.Title, "Details", "Content", new { Id = content.Id }, null);
但我得到一个空指针异常。为第一个参数创建 HtmlHelper 实例并不容易。
我有什么选择?作为 API 的一部分,我是否坚持向他们提供对所有控制器的完全访问权限?难道就没有办法提供一个简化的API供他们消费吗? (我并不是试图阻止他们编写 Razor 代码;只是封装和简化调用,并消除对我的控制器和模型的潜在依赖。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一些可能的选项:
a) 您可以使用自己的类扩展 HtmlHelper
b) 跨多个视图重用 @helpers
c) 更改视图的基本类型
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
提供的 html 帮助程序非常基本 - 但您当然可以创建一个帮助程序。请注意 - 现有的 API 已提供并且与 MVC 编程模型的其余部分(Ajax 和 html 帮助程序以及路由语法)完全吻合。
问题在于如何调用代码。请发布您的所有代码,以便我们检查。
问题是你到底想做什么?你想创建一个助手
EditLinkFor - 但是出现了问题,我们可以看到所有代码吗?
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?