Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi,
I'm currently experimenting with umbraco and found the Umbraco.NiceUrl(int) helper and wanted to write my own NiceUrl(string) helper.
So i created the below extension
public static class UmbracoExtensions { public static string NiceUrl(this UmbracoHelper helper, string cTypeAlias) { var node = helper.TypedContentAtRoot().DescendantsOrSelf<IPublishedContent>() .Where(x => x.Name == cTypeAlias).FirstOrDefault(); if (node != null) { return node.Url; } return "/"; } }
And it works fine if i put a @using at the top of the view.
My question is how do i get this to automatically be a part of the UmbracoHelper class without using a @using at the top of every view?
Would it be at application start?
Hi Sean,
You have a couple of options to this really.
The first (Not recommended but is an option):
Change the namespace of your extension method to be the same as the UmbracoHelper.
The second (a much better idea):
Edit the Web.Config file found in the Views folder. In there you will see a section that looks like this:
<namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="Umbraco.Web" /> <add namespace="Umbraco.Core" /> <add namespace="Umbraco.Core.Models" /> <add namespace="Umbraco.Web.Mvc" /> <add namespace="umbraco" /> <add namespace="Examine" /> </namespaces>
All you need to do is add your namespace into that list and it will be included in all of your views instead of you typing it out all the time.
Nik
Comment author was deleted
You can place the namespace it in the /views/web.config that way you won't need to place it in each view...
Thank you both,
i can't believe i didn't think of that myself. I was trying to be too clever with it all lol.
Thanks again.
VS2019 doesn't like it. It does compile but shows a bunch of errors.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
How do i extend the UmbracoHelper?
Hi,
I'm currently experimenting with umbraco and found the Umbraco.NiceUrl(int) helper and wanted to write my own NiceUrl(string) helper.
So i created the below extension
And it works fine if i put a @using at the top of the view.
My question is how do i get this to automatically be a part of the UmbracoHelper class without using a @using at the top of every view?
Would it be at application start?
Hi Sean,
You have a couple of options to this really.
The first (Not recommended but is an option):
Change the namespace of your extension method to be the same as the UmbracoHelper.
The second (a much better idea):
Edit the Web.Config file found in the Views folder. In there you will see a section that looks like this:
All you need to do is add your namespace into that list and it will be included in all of your views instead of you typing it out all the time.
Nik
Comment author was deleted
You can place the namespace it in the /views/web.config that way you won't need to place it in each view...
Thank you both,
i can't believe i didn't think of that myself. I was trying to be too clever with it all lol.
Thanks again.
VS2019 doesn't like it. It does compile but shows a bunch of errors.
is working on a reply...