The way you are using it is dangerous. Imagine some of your constants contained a quote, or even worse some other dangerous characters => that would break your javascripts.
I would recommend you writing a controller action which will serve all constants as javascript:
public ActionResult Constants()
{
var constants = typeof(Constants)
.GetFields()
.ToDictionary(x => x.Name, x => x.GetValue(null));
var json = new JavaScriptSerializer().Serialize(constants);
return JavaScript("var constants = " + json + ";");
}
and then in your layout reference this script:
<script type="text/javascript" src="@Url.Action("Constants")"></script>
Now whenever you need a constant in your scripts simply use it by name:
<script type="text/javascript">
alert(constants.T_URL);
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…