C# 属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式
下面是一个常量类,我用来调用一些帮助程序:C# 属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式,c#,string,asp.net-mvc-3,C#,String,Asp.net Mvc 3,下面是一个常量类,我用来调用一些帮助程序: public static class SecurityHelpers { public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; } 下面是我如何在MVC3 web应用程序的一个窗体中调用它: @using (Html.BeginForm("Index", "Checkout", FormMethod.Post)) {
public static class SecurityHelpers
{
public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";
}
下面是我如何在MVC3 web应用程序的一个窗体中调用它:
@using (Html.BeginForm("Index", "Checkout", FormMethod.Post))
{
<input type="hidden" name="amount" value="@Model.PackageCost"/>
<input type="hidden" name="currency" value="$"/>
<input type="hidden" name="itemdescription" value="@Model.PackageDescriptor"/>
<input type="hidden" name="type" value="digital"/>
@Html.AntiForgeryToken(App.WebUI.Helpers.SecurityHelpers.AntiforgeryTokenSalt)
<input type="submit" value="Confirmar" class="btn primary frmsubmit" />
}
错误在我的控制器中触发,它表示:
属性参数必须是常量表达式,typeof表达式
或属性参数类型的数组创建表达式
你知道为什么这样不行吗?ValidateAntiForgeryToken decorator的
Salt
属性是一个字符串,而我的常量也是一个字符串,所以我很困惑。静态字符串不是常量
试着改变
public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";
到
public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";
public const string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";