3

I have a base class which implements the IRouteConstraint interface. I'm trying to extend that class and override the Match() method defined in the base class. This is for an ASP.NET MVC5 application using Visual Studio 2013 Update 2.

My classes are structured like this:

\Project\App_Code\BaseRouteConstraint.cs:

namespace Website {
    public class BaseRouteConstraint: IRouteConstraint {
        public virtual bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
            ...code
        }
    }
}

\Project\App_Code\DerivedRouteConstraint.cs:

namespace Website {
    public class DerivedRouteConstraint: BaseRouteConstraint {
        public override bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
            if(!base.Match(httpContext, route, values, routeDirection) {
                ...code
            }
        }
    }
}

But I can't actually use the DerivedRouteConstraint class because Visual Studio says it doesn't exist. I also get this warning:

'Website.BaseRouteConstraint' in 'C:\Project\App_Code\BaseRouteConstraint.cs' conflicts with the imported type 'Website.BaseRouteConstraint' in 'C:\Project\'. Using the type defined in 'C:\Project\App_Code\BaseRouteConstraint.cs'

I've checked and I haven't accidentally copied the source code file for my base class anywhere else in the solution.

Mike D.
  • 4,034
  • 2
  • 26
  • 41

1 Answers1

5

Unbeknownst to me the App_Code folder doesn't work like a normal folder. I thought it was just a place to put any semi-random classes that didn't necessarily need their own folder in the project structure. While I was writing up this question I decided to try moving the files to a different (normal) folder and everything started working correctly.

The App_Code folder has special functionality and everything in it is compiled into a DLL and automatically referenced to the project. The "App Code" section on the MSDN page Shared Code for more information about this folder. This forum post (http://forums.asp.net/t/1026147.aspx?+What+is+App_Code+Folder+for+) clued me in that the code is actually referenced back to the project, which is important to understanding the error.

The root of the problem was that I had changed the namespace that the classes resided in because I didn't want them to be in the Website.App_Code namespace. So when the files in the App_Code folder were compiled and automatically referenced to the project I then had two definitions in the Website namespace for the BaseRouteConstraint class. (One from the reference App_Code DLL and one in the code itself.)

Changing the namespace back to Website.App_Code worked once I had also set the build option on the files to Compile instead of Content (based on this question: Classes residing in App_Code is not accessible). However, in this case I think it is more appropriate to move the files to their own normal folder in the project structure.

Community
  • 1
  • 1
Mike D.
  • 4,034
  • 2
  • 26
  • 41