ASP.NET MVC 如何从控制器访问 Global.asax 文件中的属性?

发布于 2024-10-29 05:30:57 字数 67 浏览 3 评论 0原文

在 Global.asax 文件中,我管理一些线程,并且 - 从控制器 - 我需要调用该线程的事件。是否可以访问该线程?

in the Global.asax file, I manage some threads, and - from the Controller - I need to invoke an event of the one's thread. Is it possible to have access to that thread ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-11-05 05:30:57

您可以使用应用程序状态来存储一些将在应用程序的所有用户之间共享的对象:

protected void Application_Start()
{
    Application["foo"] = "bar";
    ...
}

并且在控制器内您可以访问此属性:

public ActionResult Index()
{
    var foo = HttpContext.Application["foo"] as string;
    ...
}

You could use the application state to store some object that will be shared among all users of the application:

protected void Application_Start()
{
    Application["foo"] = "bar";
    ...
}

and inside your controller you can access this property:

public ActionResult Index()
{
    var foo = HttpContext.Application["foo"] as string;
    ...
}
妖妓 2024-11-05 05:30:57

如果它是任何其他类型的对象(例如字符串),您也可以这样做,因为您需要在 Global.asax 中将该属性声明为静态,以使其可供应用程序的其余部分使用

public class Application : HttpApplication
{
    // This is the class declared in Global.asax

    // Your route definitions and initializations are also in here

    public static string MyProperty { get; set; }
}

:将可供应用程序的其余部分使用。您可以通过以下方式进行调用:

public ActionResult MyAction()
{
    var bla = Application.MyProperty;
}

也就是说,我认为您不想以这种方式为应用程序的其余部分提供 Thread 。

You could if it were any other kind of object, like a string, because you'll need to declare the property as static in the Global.asax to make it available to the rest of the app:

public class Application : HttpApplication
{
    // This is the class declared in Global.asax

    // Your route definitions and initializations are also in here

    public static string MyProperty { get; set; }
}

This will be available to the rest of the application. You can call by doing:

public ActionResult MyAction()
{
    var bla = Application.MyProperty;
}

That said, I dont think you want to make a Thread available to the rest of the app this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文