C#中的全局变量替换?

C#中的全局变量替换?,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有一个MVC4/C#/.NET网站,是别人建立的,我正在管理。我有一个家庭控制器和一个家庭控制器。我正在寻找一种方法,在一个地方定义一篇文章的名称,并在所有地方使用它。特别是在Index.cshtml(通过HomeController调用)和所有文章(Article_1、Article_2等)中,通过ArticleController.cs调用 我最初的计划是定义一个全局变量(我曾经设置它): 家庭控制器: using System; using System.Collections.Gene

我有一个MVC4/C#/.NET网站,是别人建立的,我正在管理。我有一个家庭控制器和一个家庭控制器。我正在寻找一种方法,在一个地方定义一篇文章的名称,并在所有地方使用它。特别是在Index.cshtml(通过HomeController调用)和所有文章(Article_1、Article_2等)中,通过ArticleController.cs调用

我最初的计划是定义一个全局变量(我曾经设置它):

家庭控制器:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyStie.Controllers
{
    public class Globals
    {
        public static article_1_title = "This will show up all over the place";
    }
    public class HomeController : Controller //This is basically the same as ArticleCont.
    {
        public ActionResult Index()
        {
            retrurn View();
        }
    }
}
<div>@{Write(Globals.story_1_title)}</div>
Index.cshtml:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyStie.Controllers
{
    public class Globals
    {
        public static article_1_title = "This will show up all over the place";
    }
    public class HomeController : Controller //This is basically the same as ArticleCont.
    {
        public ActionResult Index()
        {
            retrurn View();
        }
    }
}
<div>@{Write(Globals.story_1_title)}</div>
@{Write(Globals.story_1_title)}
但这不起作用,所以如果你认为环球杯是一条路,让我知道我做错了什么


但是,在阅读时,基本上说依赖全局变量对C#不利,我想知道全局变量是否是一条出路。如果您同意这一点,我应该如何实现这一点?

也许我误解了您想要的内容,但为什么不使用您的web.config文件保存您需要的静态信息并从中引用它呢

Web.config

<configuration>
   <appSettings>
       <add key="ArticleTitle" value="This will show up everywhere"/>
   </appSettings>
</configuration>

为什么不使用
@Globals.story\u 1\u title
?什么叫“那不起作用”?我猜你的问题是你有一个神秘的“写”方法…+1,但这似乎是一个完整的“duh”概念。当我读到这个问题时,我在想,‘呃,为什么这不是一个配置设置?’@JoelEtherton-我也有同样的感觉,几乎没有把它作为一个答案,因为我认为我读得不对。然而,我知道我有时忘记了“更简单”的做事方式:)