【问题标题】:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
【发布时间】:2011-12-10 04:40:27
【问题描述】:

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

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 属性是一个字符串,而我的常量也是一个字符串,所以我很困惑。

【问题讨论】:

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


    【解决方案1】:

    静态字符串不是常量。

    尝试改变

    public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 
    

    public const string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 
    

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2013-02-09
      相关资源
      最近更新 更多