1

So I am doing little refactoring in our Web API project and I noticed that it will be good to extract one of the controllers in abstract class with all needed attributes so what I did is:

  • extracted abstract class and method out of my existing controller:

    [RoutePrefix("api/auth")]
    public class AdminController : BaseAdminController
    

So BaseAdminController looks like this:

public abstract class BaseAdminController : BaseApiController
{
    [HttpGet]
    [Route("validatetoken")]
    public abstract IHttpActionResult ValidateToken(string token);
}

This is the routing I use:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services
    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/"
    );
}

Now I noticed that when testing the code with abstract controller it gives throws:

No type was found that matches the controller named 'auth'

While same implementation but without abstract class and method works just fine.

I am testing with this url :

http://localhost:60747/api/auth/validatetoken?auth_token=117a2686-dad3-4b1e-a5eb-94aebed45d06
Nkosi
  • 235,767
  • 35
  • 427
  • 472
kuskmen
  • 3,648
  • 4
  • 27
  • 54

1 Answers1

0

The Inherited attribute controls whether subclasses inherit any particular attribute from a superclass. The default value is true.

Unfortunately for your refactoring, both the RouteAttribute and RoutePrefixAttribute attributes are sealed classses with Inherited = false. You'll need to specifically declare those attributes on all your concrete controller classes and methods.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Edmund Schweppe
  • 4,992
  • 1
  • 20
  • 26
  • I don't get it .. my `RoutePrefix` attribute is placed on my concrete class, whatelse is that I decompiled `Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll` (which was the assembly my project is using for this attribute) and RouteAttribute was marked with `Inherited = true` I am kinda surprised that msdn shows the opposite.. – kuskmen Apr 11 '17 at 13:06
  • This answer is misguided. review linked duplicate. – Nkosi Apr 11 '17 at 13:16