属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

时间:2011-10-18 14:56:55

标签: c# string asp.net-mvc-3

这是我用来调用一些助手的常量类:

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" />
}

在我的控制器中:

[HttpPost]
[ValidateAntiForgeryToken(Salt = SecurityHelpers.AntiforgeryTokenSalt)]
public ActionResult Index(decimal amount, string currency, string itemDescription, string type)
{
    if (!User.Identity.IsAuthenticated) return RedirectToAction("LogOn", "Account");
}

错误是在我的控制器中触发的,它说:

  

属性参数必须是常量表达式,typeof表达式   或属性参数类型

的数组创建表达式

为什么这不起作用的任何想法? ValidateAntiForgeryToken装饰器的Salt属性是一个字符串,我的常量也是一个字符串,所以我很困惑。

1 个答案:

答案 0 :(得分:23)

静态字符串不是常量。

尝试更改

public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 

public const string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 
相关问题