TIKSN
TIKSN

Reputation: 615

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

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

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.

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

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());
        }
    }

Upvotes: 1

Views: 38216

Answers (3)

Alex Z
Alex Z

Reputation: 1442

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

Add this to filterConfig.cs

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

And this to Global.ascx.cs:

FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);

Upvotes: 1

Brian Quinn
Brian Quinn

Reputation: 67

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.

Upvotes: 1

TIKSN
TIKSN

Reputation: 615

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

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.

Upvotes: 0

Related Questions