3

With the help of several online tutorials, like this one, I am still struggling to add a Web API service to an existing Asp site, that is not MVC.

I added to the project a new item of type Web API Controller Class(v2.1), named it something like AbcController.cs, and VS2015 asked me to put it in the App_Code directory. The default code has handlers for Get, Put etc. Sounded to me like I am on the right track.

I added a default route in Global.asax.cs like in the tutorial:

        RouteTable.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

This got built after adding a reference to System.Web.Http.Webhost which was not mentioned in the tutorial. Sounded like I was still on the right track.

However, it doesn't work. I run the site in debug and this gives me a 404 Not Found:

http://localhost:54905/api/abc

I tried to run this on the production server with IIS7, of course as a second test web site to not interfere with the version that is in production. However, I ran into the error that the Microsoft.Web.Infrastructure dll could not be found. To fix this, I should install MVC packages, which I don't like for just an experiment.

My questions are:

  • do I get it right that the URL is in lower case, i.e., not .../api/Abc ?

  • does this kind of routing work in the debugger?

  • am I essentially turning the web site into an MVC web site?

  • is this really the simplest way to add a "REST" service to an existing web site? I only need to implement the POST, read and return some JSON data, and do not need arguments in the URL

Jongware
  • 22,200
  • 8
  • 54
  • 100
Roland
  • 4,619
  • 7
  • 49
  • 81
  • Is it a traditional asp.net web form website? – Blaise Jul 11 '16 at 12:55
  • Indeed. However it seems as if the new Web Api controller makes it into an MVC site – Roland Jul 11 '16 at 13:09
  • As you tagged this question with asp.net-3.5 the simple answer is that Web Api requires .net 4. – Marcus Höglund Jul 11 '16 at 13:11
  • 1
    @MarcusH Good point. The msdn docs also mention .Net3.5 for REST Web API calls, our current site uses .Net3.5, I am currently partly developing in .Net 4, and upgrading the entire site to .Net4 would not be a problem. I changed the tag of this question to .Net 4. – Roland Jul 11 '16 at 13:21
  • 3
    Use a new site, in a subdomain like api.domain.com and do separate work. Its my advice. Don't mix a rendering side (ASP.NET classic) with an API software (ASP.NET Web API). Do the things more cleaner – Alberto León Jul 11 '16 at 13:24
  • Sounds like you're trying to create a hybrid app that is both web forms and web API. You will be better off separating them into two projects and configure IIS to run your API project beneath the http://localhost:54905/api/ endpoint, and your main website beneath http://localhost:54905/ – Crwydryn Jul 11 '16 at 13:30
  • 1
    @AlbertoLeón Well, actually, I like that. Why make one big piece of software? This would be a good reason to split off new functionality to a new web app. No risk to the existing stuff if I am going MVC or whatever new concepts for the new functionality. – Roland Jul 11 '16 at 13:31

0 Answers0