kol
kol

Reputation: 28678

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.

Upvotes: 4

Views: 10063

Answers (2)

khellang
khellang

Reputation: 18102

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.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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.

Upvotes: 10

Related Questions