gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:31:41 41 4
gpt4 key购买 nike

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

public static class SecurityHelpers
{
public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";
}

下面是我如何在我的 MVC3 网络应用程序中以我的一种形式调用它:

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

在我的 Controller 中:

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

错误在我的 Controller 中触发,它说:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

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

最佳答案

静态字符串不是常量。

尝试改变

public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 

public const string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 

关于c# - 属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809337/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com