Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.RouteUrl from an Html helper extension

Tags:

c#

asp.net-mvc

I'm extending Html.HtmlHelper to render the breadcrumb depending on the page I am:

public static HtmlString RenderBreadCrumb(this HtmlHelper helper, PageType pagetype, object Model)

How do I use Url.RouteUrl inside of an HtmlHelper to render the URL?

like image 514
Jose3d Avatar asked Apr 08 '11 15:04

Jose3d


2 Answers

You need to create a new UrlHelper instance using a request context.

UrlHelper Url = new UrlHelper(helper.ViewContext.RequestContext);
Url.RouteUrl(…);
like image 93
detaylor Avatar answered Sep 22 '22 01:09

detaylor


Just make a UrlHelper extension instead:

public static string RenderBreadCrumb(this UrlHelper helper, PageType pagetype, object Model)

Then call via:

 @Url.RenderBreadCrumb(pageType, Model)
like image 43
Ted Avatar answered Sep 20 '22 01:09

Ted