November Happy Hour will be moved to Thursday December 5th.

Cannot get Url.ContentUrl with my custom Html Helper

Vote:
 

Hi,

I'm working with EPiServer since september now and I'm using more advanced features now but I'm stuck with something and I'd really love if someone can help me.

I have made a custom HTML Helper to use with Razor MVC to be able to use display options without having to create a custom ContentAreaRendered and everything that seems to go with it but any links I use in my view are internal ugly url (aspx stuff) and if I have any method that are supposed to transform this ugly URL into a friendly URL it does not work.

Here is part of my block model:

        [CultureSpecific]
        [Display(
            Name = "Link",
            Description = "Link for the button",
            GroupName = SystemTabNames.Content,
            Order = 30)]
        [BackingType(typeof(PropertyUrl))]
        public virtual Url URL { get; set; }

        [CultureSpecific]
        [Display(
            Name = "Image (Optional)",
            Description = "An optional image to display under the description",
            GroupName = SystemTabNames.Content,
            Order = 40)]
        [UIHint(UIHint.Image)]
        public virtual ContentReference Image { get; set; }

Here is my Helper:

public static XhtmlString RowFromArea(this HtmlHelper html, Expression> expression)
{
    var returnString = "";
    var area = ModelMetadata.FromLambdaExpression(expression, html.ViewData).Model as ContentArea;
    if (area != null)
    {
        returnString += "
"; foreach (ContentAreaItem item in area.FilteredItems) { var block = item.GetContent(); // LoadDisplayOption(item); returnString += "
" + html.PropertyFor(x => block) + "
"; } returnString += "
"; //var urlResolver = ServiceLocator.Current.GetInstance(); //urlResolver.GetVirtualPath(new ContentReference()); } return new XhtmlString(returnString); }

Here is the page view where I call the actual helper:

@{
    Layout = "~/Views/Shared/Layouts/_Layout.cshtml";
}
@Html.RowFromArea(x => x.BottomArea)
@Html.RowFromArea(x => x.RightArea)

And this is what I have in my block partial view:

    

You see I have an image that is a ContentReference and a Link that is can be either to an internal page or an external URL. And I get only ugly URL. I'm using the same 

@Url.ContentUrl(Model.URL) in another view that is not passing through my helper and it's working properly so I think the problem is coming from something I missed in my helper.

Thank you.
#118132
Feb 27, 2015 14:52
Vote:
 

Hi,

Just a small message to say I'm still looking for a solution for this.

Thanks.

#118741
Mar 12, 2015 16:54
Vote:
 

For info, I just fixed my bug. My helper was returning a XhtmlString and apparently for the UrlHelper to properly do its jobs you need to return a MvcHtmlString.

Took me two months but it's fixed :D

#121631
May 15, 2015 20:52
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.