ASP.NET MVC 用に独自の HtmlHelper 拡張機能を作成しています。
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
がありませんか?それとも新しいインスタンスを作成する必要がありますか? オーバーヘッドはあまりないと思いますが、可能であれば既存のものを使用したいと思います。