相关疑难解决方法(0)

如何从ActionFilter访问ModelState?

我正在构建一个ActionFilter重用一些代码用于简单的垃圾邮件块 - 基本上我所做的是我有一个Html Helper方法来呈现输入文本框和一个隐藏的输入,并在ActionFilter中我检查这两个值是否相同.如果没有,我想利用我的验证逻辑的其余部分并添加一个ModelStateErrorModelState,但我怎么做呢?如何添加一个ModelStateError来自whithin ActionFilter

更新:这是我正在尝试的代码.当我测试具有此属性的控制器操作时,即使我没有传递任何所需的表单值,ModelState.IsValid仍然会返回true:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var r = filterContext.HttpContext.Request;
    if (r.Form["sixtimesnine"] != r.Form["fourtytwo"] || string.IsNullOrEmpty(r.Form["sixtimesnine"]) || string.IsNullOrEmpty(r.Form["fourtytwo"]))
    {
        filterContext.Controller.ViewData.ModelState.AddModelError("Spam", this.ErrorMessage);
    }
    base.OnActionExecuting(filterContext);
}
Run Code Online (Sandbox Code Playgroud)

这是ActionMethod:

[ValidateAntiSpam(ErrorMessage = "Spambotar får inte.")]
public ActionResult Write(GuestbookPost postToCreate)
{
    if (ModelState.IsValid)
    {
        _posts.Add(postToCreate);
        return RedirectToAction("Index");
    }
    return View();
}
Run Code Online (Sandbox Code Playgroud)

我只是注意到,如果我在OnActionExecuting方法中设置了一个断点并点击"Debug tests",那么断点就永远不会被击中.为什么?

asp.net-mvc modelstate action-filter

42
推荐指数
1
解决办法
1万
查看次数

在MVC4中显示和错误,我必须实现一些接口,但我已经完成了它

我正在尝试创建自己的过滤器属性,以支持多语言.这个想法很简单.URL代表语言.

  • *HTTP://host.ext/ EN/rest_of_the_url*将用英语开
  • *http://host.ext/ hy/rest_of_the_url*将以亚美尼亚语开放.

问题是在运行时它会说MultilingualActionFilterAttribute

以下是错误文本"给定的过滤器实例必须实现以下一个或多个过滤器接口:IAuthorizationFilter,IActionFilter,IResultFilter,IExceptionFilter".

在这里,我将它用作全局过滤器.

namespace TIKSN.STOZE.WebApp
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(System.Web.Mvc.GlobalFilterCollection filters)
        {
            filters.Add(new TIKSN.STOZE.Common.MultilingualActionFilterAttribute());
            filters.Add(new System.Web.Mvc.HandleErrorAttribute());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里定义它.

namespace TIKSN.STOZE.Common
{
    public class MultilingualActionFilterAttribute : System.Web.Mvc.ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            string language = System.Convert.ToString(filterContext.RouteData.Values["language"]);

            System.Diagnostics.Debug.Print("Requested language is '{0}'", language);
            language = Helper.PickUpSupportedLanguage(language);
            System.Diagnostics.Debug.Print("Supported language is '{0}'", language);

            if (language == string.Empty)
            {
                filterContext.HttpContext.Response.RedirectToRoutePermanent(new { language = Common.Properties.Settings.Default.DefaultLanguageCode });
            }

            language = Helper.TryToPickUpSupportedLanguage(language);

            System.Threading.Thread.CurrentThread.CurrentCulture = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc multilingual inheritance interface-implementation

11
推荐指数
2
解决办法
8229
查看次数