4

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.

2
  • 2
    It's not clear what you are trying to achieve here or what your problem is. Why don't you want to use VS? Commented 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. Commented Sep 16, 2013 at 10:35

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. Actually I don't want to create the same structure: for example, I don't need server-side MVC and Razor code. I would like to understand what is needed to create only the RESTful web service (models, controllers, and ...?).
@kol, I have updated my answer to illustrate the minimal steps to create an ASP.NET Web API application hosted in an ASP.NET app host using tools from the prehistory of software development.
Thanks for the list of steps, I'm going to try them right now.
I compiled bin\MvcApplication1.dll. Now how can I test the API? When I try to open 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.
OK, it works now. I added a wildcard application map: haacked.com/archive/2008/11/26/…
|
2

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.

1 Comment

Thanks a lot! I've never heard of scriptcs before. Looks like a very useful tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.