c# - 这个 html 辅助线程安全吗?

标签 c# asp.net-mvc-3 thread-safety

我想知道作为此链接上接受的答案引用的代码是否是线程安全的。我的意思是不适用于多线程。我只是不想输出跨用户页面请求。
Add CSS or JavaScript files to layout head from views or partial views

我是否会遇到这样一种情况,即对一个页面的许多请求可能会跨越样式和脚本。

如果您了解 MVC,这可能会有所帮助,因为在呈现 View 时调用添加方法并将结果呈现到布局(母版页)。

当前解决方案(如果需要改进请告诉我)

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;
}

谢谢

最佳答案

嗯....我觉得不像;p

HtmlHelper 有一些实例属性,特别是 ViewContextViewData(通过 ViewDataContainer)等。 anywhere static 是一个糟糕透顶的想法。

使用正在执行的基本代码,您可能 可以摆脱它,但是:IMO 这仍然是一个非常糟糕的主意。发现得很好。

关于c# - 这个 html 辅助线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6609586/

相关文章:

c# - 无法将类型 System.LinQ.IOrderedEnumerable 隐式转换为 MyClassCollection。存在隐式转换(您是否缺少强制转换?)

c# - 创建 Windows 应用商店应用程序时出现问题。 (Windows 8)

c# - 仅可测试性就可以证明依赖注入(inject)的合理性吗?

asp.net-mvc-3 - 在 ASP.NET MVC 3 中增加超时值

c# - 将通用 IDictionary 转换为 ASP.NET MVC IEnumerable<SelectListItem> : choosing the Selected item

c# - 如何使用 .net TransactionScope 从 MQ 执行事务性 GET?

c# - 在 NUnit 测试中使用最小起订量模拟 HttpContext.Current

scala - 从现有的一个线程安全生成一个新的 actor 吗?

java - "inherently thread-safe"是什么意思?

java - 是否应该在 EDT 内声明按键绑定(bind)?