Is it possible to create a RESTful web service using ASP.NET WebAPI by hand, I mean, without Visual Studio? What are the required files and how should I install them on the server? Say, I would like to reproduce this example.
-
2It's not clear what you are trying to achieve here or what your problem is. Why don't you want to use VS?Daniel Hilgarth– Daniel Hilgarth2013-09-16 10:22:00 +00:00Commented Sep 16, 2013 at 10:22
-
2@DanielHilgarth When I'm learning a new technology (in this case, WebAPI) I always want to understand how it can be used using only a simple text editor and the command-line compiler. After that, I switch back to the IDE, but I don't think it safe relying on IDE wizardry without understanding what's going on in the background.kol– kol2013-09-16 10:35:00 +00:00Commented Sep 16, 2013 at 10:35
2 Answers
Is it possible to create a RESTful web service using ASP.NET WebAPI by hand, I mean, without Visual Studio?
Of course that it's possible. All you have to do is use notepad or any other text editor to recreate the same structure and then manually compile using the csc.exe compiler which is part of the framework.
Here are the trivial steps:
notepad.exe ValuesController.cs:public class ValuesController : System.Web.Http.ApiController { public object Get(string id) { return string.Format("id: {0}", id); } }notepad.exe Global.asax.cs:using System.Web.Http; public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { GlobalConfiguration.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = System.Web.Http.RouteParameter.Optional } ); } }notepad.exe Global.asax:<%@ Application Codebehind="Global.asax.cs" Inherits="MvcApplication" Language="C#" %>notepad web.config:<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <validation validateIntegratedModeConfiguration="true" /> <modules runAllManagedModulesForAllRequests="true" /> <handlers> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> </system.webServer> </configuration>mkdir bincsc /target:library /out:bin\MvcApplication1.dll /reference:"c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.dll" /reference:"c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.WebHost.dll" /reference:"c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.dll" ValuesController.cs Global.asax.csPoint your web server to the root directory containing the
web.configfile and you are good to go.
This being said, you have to be completely out of your mind to do that, but anyway, you can do it.
6 Comments
http://localhost:8080/api/values or .../values/1, I get an error 404. If I put an index.html into the folder of web.config, then I can open it with http://localhost:8080/index.html.It should be pretty easy using scriptcs. If you use the Web API script pack, all you need to write is the controllers. See Glenn Block's sample code here. He's also showing it live in action at the end of this talk.