简体   繁体   中英

How to create RESTful web service using ASP.NET WebAPI without Visual Studio?

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 .

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:

  1. notepad.exe ValuesController.cs :

     public class ValuesController : System.Web.Http.ApiController { public object Get(string id) { return string.Format("id: {0}", id); } } 
  2. 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 } ); } } 
  3. notepad.exe Global.asax :

     <%@ Application Codebehind="Global.asax.cs" Inherits="MvcApplication" Language="C#" %> 
  4. 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> 
  5. mkdir bin

  6. csc /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.cs

  7. Point your web server to the root directory containing the web.config file 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM