{"id":7952,"date":"2015-11-12T03:16:36","date_gmt":"2015-11-12T03:16:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/net-webapi-attribute-routing-and-inheritance-open-source-projects-odata-webapi\/"},"modified":"2015-11-12T03:16:36","modified_gmt":"2015-11-12T03:16:36","slug":"net-webapi-attribute-routing-and-inheritance-open-source-projects-odata-webapi","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/net-webapi-attribute-routing-and-inheritance-open-source-projects-odata-webapi\/","title":{"rendered":".NET WebAPI Attribute Routing and inheritance-open source projects OData\/WebApi"},"content":{"rendered":"<p>I am playing around with the idea of having a base controller that uses a generic repository to provide the basic CRUD methods for my API controllers so that I don&#8217;t have to duplicate the same basic code in each new controller. But am running into problems with the routing attribute being recognized when it&#8217;s in the base controller. To show exactly what the problem I&#8217;m having I&#8217;ve created a really simple WebAPI controller.<\/p>\n<p>When I have the Get method in the main Controller and it inherits from the ApiController directly I don&#8217;t have any problems and this works as expected.<\/p>\n<pre><code>[RoutePrefix(\"admin\/test\")]\npublic class TestController : ApiController\n{\n    [Route(\"{id:int:min(1)}\")]\n    public string Get(int id)\n    {\n        return \"Success\";\n    }\n}\n<\/code><\/pre>\n<p>When I move the Get method into a base controller it is returning the contents of the 404 page.<\/p>\n<pre><code>[RoutePrefix(\"admin\/test\")]\npublic class TestController : TestBaseController\n{\n\n}\n\npublic class TestBaseController : ApiController\n{\n    [Route(\"{id:int:min(1)}\")]\n    public string Get(int id)\n    {\n        return \"Success\";\n    }\n}\n<\/code><\/pre>\n<p>Some more interesting notes:<\/p>\n<ul>\n<li>\n<p>I can access the action at GET \/Test\/1. So it is finding it based on the default route still.<\/p>\n<\/li>\n<li>\n<p>When I try to access POST \/admin\/test it returns the following JSON<\/p>\n<p>{ &#8220;Message&#8221;:&#8221;No HTTP resource was found that matches the request URI &#8216;http:\/\/test.com\/admin\/test&#8217;.&#8221;, &#8220;MessageDetail&#8221;:&#8221;No type was found that matches the controller named &#8216;admin&#8217;.&#8221; }<\/p>\n<\/li>\n<\/ul>\n<p>Does anyone know of a way to get the routing to work with attributes from a base controller?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am playing around with the idea of having a base controller that uses a generic repository to provide the basic CRUD methods for my API controllers so that I don&#8217;t have to duplicate the same basic code in each new controller. But am running into problems with the routing attribute being recognized when it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7952","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7952"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7952\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}