I am writing my own HTML extension to ASP.NET MVC:
Public static string CreateDialogLink (this HtmlHelper html helper, string link text, string content path) {// fix the content path if the user provided the path beginning with '~' contentPath = Url.Content (contentPath); // does not work (see below for this) // link and return it .........;};
Where I'm having trouble, trying to reach inside from UrlHelper
from the definition of my HMLHlper. The problem is that the way you usually reach through a property on the scene, via HtmlHelper
( Html.MethodName (...)
). This is not clearly available to me from my extension class.
This is the actual MVC source code for ViewMasterPage
(according to beta) - which defines html
and url
.
See public classMaster page: Master page {public ViewMasterPage (); Public AjaxHelper Ajax {Receive; } Public HtmlHelper HTML {get; } Public Object Models {get; } Public TempDataDictionary TempData {get; } Public UrlHelper Url {get; } Receive Public ViewerText ViewClean { } Public ViewDataDictionary ViewData {get; } Public HtmlTextWriter Writer {get; }}
I want to be able to use these properties inside a HtmlHelper.
The best I've come up with is this (enter the beginning of the code> CreateDialogLink method)
HtmlHelper HTML = New HtmlHelper (htmlHelper.ViewContext , HtmlHelper.ViewDataContainer); URLHLper URL = New URL Helper (HTML helper. VOICNCTX request);
Am I missing some other way to access existing HtmlHelper
and UrlHelper
examples - or do I really Need to create a new one? I'm sure there is not much upper part, but I would like to use the ones I used earlier to do if I can.
Before I asked this question, I had seen some MVC source code, but obviously I remembered it, that they Image do this for the Authors. It seems that a new Update: According to the comments the RTM code is slightly different. File: MVC \ src \ MvcFutures \ Mvc \ ImageExtensions.cs
[System.Diagnostics. CodeAnalysis.SuppressMessage ("Microsoft.Design", "CA1054: UriParametersShouldNotBeStrings", MessageId = "1 #", justification = "return value can not be a normal URL since it is ~ / ASP.NET special characters")] Public static string image (this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary & lt; string, object & gt; htmlAttributes) {if (String.IsNullOrEmpty (imageRelativeUrl)) {New ArgumentException (MvcResources.Common_NullOrEmpty, "imageRelativeUrl") ; } UrlHelper url = New UrlHelper (helper.ViewContext); String imageUrl = url.Content (imageRelativeUrl); Return Image (Image URL, Alt, HTMLAttives). Ostring (tgendermode. Self-closing); }
UrlHelper
Institiv after correct approach to all that is good enough for me.
[System.Diagnostics.CodeAnalysis.SuppressMessage ( "Microsoft.Design", "CA1054: UriParametersShouldNotBeStrings" , MessageId = "1 #", justification =)] public static string image (this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary or & lt "return value not it ~ / ASP.NET special characters after a normal URL can you throw "string, object & gt; htmlAttributes) {if (String.IsNullOrEmpty (imageRelativeUrl)) {new ArgumentException (MvcResources.Common_NullOrEmpty," imageRelativeUrl "); } UrlHelper url = new UrlHelper (helper.ViewContext.RequestContext); String imageUrl = url.Content (imageRelativeUrl); Return Image (Image URL, Alt, HTMLAttives). Ostring (tgendermode. Self-closing); }
Comments
Post a Comment