开发者

Is this html helper thread safe?

开发者 https://www.devze.com 2023-03-18 10:12 出处:网络
I am wondering if the code as referenced as the accepted answer on thi开发者_开发问答s link is thread safe. I mean not for multi threading. I just dont want output crossing user page requests.

I am wondering if the code as referenced as the accepted answer on thi开发者_开发问答s link is thread safe. I mean not for multi threading. I just dont want output crossing user page requests.

Add CSS or JavaScript files to layout head from views or partial views

Would I have a situation where many requests to a page could have crossed over styles and scripts.

It may help if you have knowledge of MVC in that the add methods are called as views are rendered and the result is rendered to the layout (master page).

Current Solution (Please let me know if it should be improved)

public static MyCompanyHtmlHelpers GetInstance(HtmlHelper htmlHelper)
{
    MyCompanyHtmlHelpers _instance;
    if (htmlHelper.ViewData["SectionHelper"] == null)
    {
        _instance = new MyCompanyHtmlHelpers();
        htmlHelper.ViewData["SectionHelper"] = _instance;
    }
    else
        _instance = htmlHelper.ViewData["SectionHelper"] as MyCompanyHtmlHelpers;

    _instance.SetHtmlHelper(htmlHelper);

    return _instance;
}

thanks


Hmm.... doesn't look like it to me ;p

HtmlHelper has some instance properties, in particular ViewContext and ViewData (via ViewDataContainer) etc. Putting that anywhere static is a terrible terrible idea.

With the basic code that is going on you'll probably get away with it, but: IMO this is still a very bad idea. Well spotted.

0

精彩评论

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