开发者

mvc create my own html helper, how can i access httpcontext?

开发者 https://www.devze.com 2022-12-27 16:33 出处:网络
I\'ve come across two recommendations for creating custom html helpers: either extend an existing on开发者_如何学JAVAe, or write your own class.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消