views:

1205

answers:

4

I am writing my own HtmlHelper extenstion for 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
            // .....
        };

Where I am having trouble is tryin to access UrlHelper from within my HtmlHelper's definition. The problem is that the way you normally access HtmlHelper (via Html.MethodName(...) ) is via a property on the View. This isn't available to me obviously from with my own extension class.

This is the actual MVC source code for ViewMasterPage (as of Beta) - which defines Html and 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; }
    }

I want to be able to access these properties inside an HtmlHelper.

The best I've come up with is this (insert at beginning of CreateDialogLink method)

HtmlHelper Html = new HtmlHelper(htmlHelper.ViewContext, htmlHelper.ViewDataContainer);
UrlHelper Url = new UrlHelper(htmlHelper.ViewContext.RequestContext);

Am I missing some other way to access the existing HtmlHelper and UrlHelper instances - or do i really need to create a new one? I'm sure there isn't much overhead but I'd prefer to use the preexisting ones if I can.

A: 

Well, you can always pass the instance of the page to the extension method. I think that is a much better way of doing this than creating new instances in your method.

You could also define this method on a class that derives from MasterPage/ViewMasterPage and then derive the page from that. This way, you have access to all the properties of the instance and don't have to pass them around.

casperOne
that definitely solves the problem of creating a new object. i only very quickly looked at the source code for new HtmlHelper() but it doesnt seem to be doing too much. i prefer that from <%= Html.CreateDialogLink(this, "Go to Yahoo", "http://www.yahoo.com") %>
Simon_Weaver
+3  A: 

I faced a similar issue and decided that it would be easier to just call the UrlHelper in the view and pass the output to my HtmlHelper extension. In your case it would look like:

<%= Html.CreateDialogLink( "text", Url.Content( "~/...path.to.content" ) ) %>

If you want to access the extension methods on the existing HtmlHelper that is passed into your class, you should only need to import System.Web.Mvc.Html in your source code file and you will get access to them (that's where the extension classes are defined). If you want a UrlHelper, you'll need to instantiate that as the HtmlHelper you are getting doesn't have a handle for the ViewPage that it's coming from.

tvanfosson
ya. thats definitely a good solution if you trust the people calling your link. see my additional answer below
Simon_Weaver
+8  A: 

Before asking this question I had looked at some of the MVC source code, but evidently I missed this, which is how they do it for the Image helper.

[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);
        }

Looks like instantiating a new UrlHelper is the correct approach after all. Thats good enough for me.


Update: RTM code from ASP.NET MVC v1.0 Source Code is slightly different as pointed out in the comments.

File: 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);
        }
Simon_Weaver
For posterity, I think instantiating `url` should look like this instead: `UrlHelper url = new UrlHelper(html.ViewContext.RequestContext);` --- its constructor takes a requestContext, not a viewContext. Not sure why this would be the case if you copied it from their source? (Perhaps this is from an earlier MVC beta release?)
Funka
A: 

If you need to create a UrlHelper in a utility class you can do the following :

string url = "~/content/images/foo.jpg";

  var urlHelper = new UrlHelper(new RequestContext(
                  new HttpContextWrapper(HttpContext.Current), 
                  new RouteData()), RouteTable.Routes);

  string absoluteUrl = urlHelper.Content(url);

This allows you to use routing or '~ expansion' away from an MVC context.

Simon_Weaver