asp.net-mvc System.Web.Mvc.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19526095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:14:26  来源:igfitidea点击:

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

asp.net-mvcfilterattributes

提问by TIKSN

I had working code on VS 2013 NET4.5. After update (VS 2013, NET 4.5.1) of NuGet packages I got this error

我在 VS 2013 NET4.5 上有工作代码。NuGet 包更新(VS 2013,NET 4.5.1)后,我收到此错误

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.

System.Web.Mvc.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

附加信息:给定的过滤器实例必须实现以下一个或多个过滤器接口:IAuthorizationFilter、IActionFilter、IResultFilter、IExceptionFilter。

I am sure that I implemented IActionFilterinterface, so how can I get error like this and how can I fix it?

我确定我实现了IActionFilter接口,那么我怎么能得到这样的错误,我该如何解决呢?

FYI:

供参考:

public class WWWActionFilterAttribute : System.Web.Mvc.ActionFilterAttribute, System.Web.Mvc.IActionFilter
    {
        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            System.Uri Address = filterContext.HttpContext.Request.Url;

            string[] domains = Address.Host.Split('.');

            if (domains.Length == 2)
            {
                System.UriBuilder AddressBuilder = new System.UriBuilder(Address);

                AddressBuilder.Host = string.Format("www.{0}", AddressBuilder.Host);

                filterContext.HttpContext.Response.Redirect(AddressBuilder.Uri.AbsoluteUri);
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }
    }

Config

配置

public class FilterConfig
    {
        public static void RegisterGlobalFilters(System.Web.Mvc.GlobalFilterCollection filters)
        {
            filters.Add(new System.Web.Mvc.HandleErrorAttribute());
            filters.Add(new TIKSN.HomeWebsite.Generalization.WWWActionFilterAttribute());
            filters.Add(new TIKSN.HomeWebsite.Globalization.LanguageActionFilterAttribute());
        }
    }

采纳答案by TIKSN

This was because of web.configfile. You have to specify exact version of packages there. Look here

这是因为web.config文件。您必须在那里指定包的确切版本。看这里

Update

更新

In Web.config all packages are registered and have their name and version pairs. The actual versions and the versions in the Web.config were not matching.

在 Web.config 中,所有包都已注册并具有其名称和版本对。实际版本与 Web.config 中的版本不匹配。

回答by Brian Quinn

Are we talking about System.Web.Mvc.dll having to have the save version and if so where? If I add this DLL to my domain I can only get up to version 4.0. However I have a version 5.1 in my web API the project with web config file. I really don't understand why I would have to match these versions and why my solution doesn't recognize the highest version if that is why its failing. I believe Visual Studio 2013 Express automatically loaded version 5.1, but I had to get my own for the domain project and that doesn't offer 5.1. One of the worst things about MVC is dependency management.

我们是在谈论 System.Web.Mvc.dll 必须有保存版本吗?如果有,在哪里?如果我将此 DLL 添加到我的域中,我只能得到 4.0.0 版本。但是,我的 Web API 中有一个 5.1 版,该项目带有 Web 配置文件。我真的不明白为什么我必须匹配这些版本以及为什么我的解决方案无法识别最高版本,如果这就是它失败的原因。我相信 Visual Studio 2013 Express 会自动加载 5.1 版,但我必须为域项目获取自己的版本,并且不提供 5.1。MVC 最糟糕的事情之一是依赖管理。

回答by Alex Z

You are adding a HttpFilter (WebAPI filter) to an MVC Global filter. Your filter should be added to HttpFilterCollection

您正在向 MVC 全局过滤器添加 HttpFilter(WebAPI 过滤器)。您的过滤器应添加到 HttpFilterCollection

Add this to filterConfig.cs

将此添加到 filterConfig.cs

public static void RegisterHttpFilters(HttpFilterCollection filters)
{
    filters.Add(new WWWActionFilterAttribute());
}

And this to Global.ascx.cs:

这是 Global.ascx.cs:

FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);