Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 需要允许操作方法字符串参数绑定标记_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 需要允许操作方法字符串参数绑定标记

Asp.net mvc 3 需要允许操作方法字符串参数绑定标记,asp.net-mvc-3,Asp.net Mvc 3,我有一个action方法,它将字符串作为它的唯一参数。action方法对其进行转换,并将结果返回给客户端(这是通过ajax调用完成的)。我需要在字符串值中允许标记。在过去,我通过使用[allowtml]装饰模型上的属性来实现这一点,但该属性不能用于参数,并且allowtmlatAttribute类是密封的,因此我无法从中继承。我目前有一个解决方案,我创建了一个只有一个属性的模型,并用前面提到的属性对其进行了修饰,这是可行的 我认为我不应该跳过那个圈。我是否缺少什么,或者我是否应该向MVC团队请求

我有一个action方法,它将字符串作为它的唯一参数。action方法对其进行转换,并将结果返回给客户端(这是通过ajax调用完成的)。我需要在字符串值中允许标记。在过去,我通过使用
[allowtml]
装饰模型上的属性来实现这一点,但该属性不能用于参数,并且
allowtmlatAttribute
类是密封的,因此我无法从中继承。我目前有一个解决方案,我创建了一个只有一个属性的模型,并用前面提到的属性对其进行了修饰,这是可行的


我认为我不应该跳过那个圈。我是否缺少什么,或者我是否应该向MVC团队请求允许在方法参数上使用此属性?

您是否查看了
ValidateInputAttribute
?此处的更多信息:。

如果需要为特定的参数允许html输入(与“模型属性”相反),则没有内置的方法,因为
[allowtml]
仅适用于模型。但您可以使用自定义模型绑定器轻松实现这一点:

public ActionResult AddBlogPost(int id, [ModelBinder(typeof(AllowHtmlBinder))] string html)
{
    //...
}
AllowTMLBinder代码:

public class AllowHtmlBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var request = controllerContext.HttpContext.Request;
        var name = bindingContext.ModelName;
        return request.Unvalidated[name]; //magic happens here
    }
}

在我的博文中找到完整的源代码和解释:

如果有一个每参数属性就更好了,这样你就可以非常精确地知道哪些字段允许有潜在的不安全值,但是出于我目前的需要,这就可以了,谢谢!当您需要只在多个参数中的一个参数中允许HTML时,这没有帮助。