{"id":4188,"date":"2016-07-31T12:38:06","date_gmt":"2016-07-31T12:38:06","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4188"},"modified":"2016-07-31T12:38:06","modified_gmt":"2016-07-31T12:38:06","slug":"adding-a-webapi-controller-to-an-existing-asp-net-mvc-application","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/adding-a-webapi-controller-to-an-existing-asp-net-mvc-application\/","title":{"rendered":"Adding a WebApi controller to an existing ASP.NET MVC application"},"content":{"rendered":"<p>So I&#8217;ve got an existing ASP.NET MVC5 application and need to add a REST api using WebApi.<\/p>\n<ul>\n<li>Add a new Controller<\/li>\n<li>Select Web API 2 Controller &#8211; Empty (or whatever your preference is)<\/li>\n<li>Add you methods as normal<\/li>\n<li>Open Global.asax.cs and near the start, for example after AreaRegistration but before the route configuration, add\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nGlobalConfiguration.Configure(WebApiConfig.Register);\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>easy enough. The key is to <strong>not<\/strong> put the GlobalConfiguration as the last line in the Global.asax.cs as I did initially.<\/p>\n<p>If we assume your controller was named AlbumsController, it might looks something like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class AlbumsController : ApiController\r\n{\r\n   \/\/ api\/albums\r\n   public IEnumerable&lt;Album&gt; GetAllAlbums()\r\n   {\r\n      \/\/ assuming albums is populated \r\n      \/\/ with a list of Album objects\r\n      return albums;\r\n   }\r\n}\r\n<\/pre>\n<p>as per the comment, access to the API will be through url\/api\/albums, see WebApiConfig in App_Start for the configuration of this URL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;ve got an existing ASP.NET MVC5 application and need to add a REST api using WebApi. Add a new Controller Select Web API 2 Controller &#8211; Empty (or whatever your preference is) Add you methods as normal Open Global.asax.cs and near the start, for example after AreaRegistration but before the route configuration, add GlobalConfiguration.Configure(WebApiConfig.Register); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[128],"tags":[],"class_list":["post-4188","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4188"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4188\/revisions"}],"predecessor-version":[{"id":4204,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4188\/revisions\/4204"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}