I would like to create a helper class which is similar to the UrlHelper class in that it requires an instantiation of the class to be available to the views, but I cant for the life of me work out where UrlHelper is instantiated to become available in the view as @Url, as I would like to do the same thing.
Can anyone enlighten me at all?
I’ve never done this before, but it’s something you could try.
Try creating a new class that extends System.Web.Mvc.ViewPage. I don’t have the MVC source on this machine, so I can’t say exactly how the framework creates this instance for the views, but you may be able to change the base page-type in your Web.config to make MVC create instances of your new type rather than its own type.
You could then add whatever properties you want to that new type and have them accessible in your views.
You’d also have to create a generic for that type:
Your.New.Page.Type<T>, and declare that as the type in your view (I’m not exactly sure how this is done in Razor):There’s probably a bunch of stuff I’m missing.
See the
pageBaseTypeattribute: