如何访问从我自己的HtmlHelper内的HtmlHelper方法呢?
-
20-08-2019 - |
题
我写我自己的HtmlHelper extenstion ASP.NET MVC的:
public static string CreateDialogLink (this HtmlHelper htmlHelper, string linkText,
string contentPath)
{
// fix up content path if the user supplied a path beginning with '~'
contentPath = Url.Content(contentPath); // doesn't work (see below for why)
// create the link and return it
// .....
};
在哪里我有麻烦的是试着从在我的HtmlHelper的定义访问UrlHelper
。问题是,你通常访问HtmlHelper
(通过Html.MethodName(...)
)的方式是通过在查看的属性。这不是提供给我从我自己的扩展类明显。
这是ViewMasterPage
实际MVC的源代码(如测试版的) - 其限定Html
和Url
public class ViewMasterPage : MasterPage
{
public ViewMasterPage();
public AjaxHelper Ajax { get; }
public HtmlHelper Html { get; }
public object Model { get; }
public TempDataDictionary TempData { get; }
public UrlHelper Url { get; }
public ViewContext ViewContext { get; }
public ViewDataDictionary ViewData { get; }
public HtmlTextWriter Writer { get; }
}
我希望能够访问一个的HtmlHelper内这些性能。
我想出来的,最好是这样(在CreateDialogLink
方法的开头插入)
HtmlHelper Html = new HtmlHelper(htmlHelper.ViewContext, htmlHelper.ViewDataContainer);
UrlHelper Url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
我错过了一些其他的方式来访问现有HtmlHelper
和UrlHelper
实例 - 或者我是否真的需要创建一个新的?我敢肯定,没有太多的开销,但如果我可以我宁愿使用预先存在的。
解决方案
问这个问题,我在一些MVC源代码的期待已久,但显然我错过了这一点,这是他们如何做到这一点的图像帮手。之前
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(imageRelativeUrl)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
}
UrlHelper url = new UrlHelper(helper.ViewContext);
string imageUrl = url.Content(imageRelativeUrl);
return Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing);
}
看起来像实例化新UrlHelper
是正确的方法毕竟。那是对我来说足够了。
<强>更新:从 ASP.NET MVC 强> RTM代码在评论中指出v1.0的源代码略有不同。
文件:MVC \ SRC \ MvcFutures \的mvc \ ImageExtensions.cs
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(imageRelativeUrl)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
}
UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext);
string imageUrl = url.Content(imageRelativeUrl);
return Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing);
}
其他提示
我遇到类似的问题,并决定它会更容易只是调用视图的UrlHelper和输出传递给我的HtmlHelper扩展。在你的情况下,它看起来像:
<%= Html.CreateDialogLink( "text", Url.Content( "~/...path.to.content" ) ) %>
如果您要访问的传递到你的类中现有的HtmlHelper扩展方法,你应该只需要在你的源代码文件中导入System.Web.Mvc.Html,您将获得对它们的访问(这就是扩展类是定义)。如果你想要一个UrlHelper,你需要实例,作为你的HtmlHelper越来越没有了的ViewPage一个手柄,它的来源。
如果你需要创建一个实用工具类UrlHelper你可以做到以下几点:
的字符串的URL = “〜/内容/图像/ foo.jpg”;
var urlHelper = new UrlHelper(new RequestContext(
new HttpContextWrapper(HttpContext.Current),
new RouteData()), RouteTable.Routes);
string absoluteUrl = urlHelper.Content(url);
这允许您使用路由或“〜膨胀”远离一个MVC上下文。
好了,你可以在页面的实例始终传递给扩展方法。我认为这是做这不是在你的方法创建新实例的一个更好的方法。
您还可以定义在从母版/ ViewMasterPage派生的类该方法,然后从该衍生的页面。通过这种方式,您可以访问该实例的所有属性,并没有通过他们周围。