I've come across two recommendations for creating custom html helpers: either extend an existing on开发者_如何学JAVAe, or write your own class.
I'd prefer to keep my custom code separated, it seems a bit sloppy to extend helpers for a decent-size application.
But the benefit I see in extending is that 'This HtmlHelper helper' is passed as a parameter, through which I can get ViewContext.HtmlContext.
My question is, how can I roll my own helper class and still have ViewContext.HtmlContext available to me?
Thanks!
Edit: What I am looking to do, is create "MyHelperClass" which will render some custom objects as html. I don't see a need to "Extend" an Html helper since i'm not using anything that it offers me. The only reason I have to extend htmlhelper currently is to access httpcontext, as you've shown. But my question was, how can i access httpcontext in my own class, without extending an existing helper. thanks
public static class HtmlHelperExtensions
{
public static HttpContextBase GetContext(this HtmlHelper htmlHelper)
{
return htmlHelper.ViewContext.HttpContext;
}
}
You might also use: System.Web.HttpContext.Current.Request.RequestContext
精彩评论