|
1 | 1 | scriptcs-webapi
|
2 | 2 | ===============
|
3 | 3 |
|
4 |
| -Script pack for web api |
| 4 | +# Web API Script Pack |
| 5 | + |
| 6 | +## What is it? |
| 7 | +Makes using ASP.NET Web API's self host with scriptcs easy as cake, much easier than [this] (https://github.com/scriptcs/scriptcs-samples/tree/master/webapihost) :) |
| 8 | + |
| 9 | +## Highlights: |
| 10 | + |
| 11 | +* Creates a pre-configured self host with the default route already added. |
| 12 | +* Configures to allow resolving script controllers. |
| 13 | +* Automatically imports common web api namespaces for you. |
| 14 | + |
| 15 | +## Getting started with Web API using the pack |
| 16 | + |
| 17 | +Disclaimer: Ultimately (soon) you will be able to install this via nuget and not have to clone / build / copy |
| 18 | + |
| 19 | +* Clone this repo (if you are seeing this before my [PR] (https://github.com/scriptcs/scriptcs/pull/177) was accepted, then be sure to first clone my [fork] (https://github.com/glennblock/scriptcs/tree/147) of scriptcs and use it. |
| 20 | +* Build the solution (make sure you enable package restore). |
| 21 | +* Create a new folder for your script i.e. c:\hellowebapi and change to it. |
| 22 | +* Install the Web Api Self Host nuget package ```scriptcs -install Microsoft.AspNet.WebApi.SelfHost``` |
| 23 | +* Copy ScriptCs.WebApi.Pack.dll from the script pack bin folder to your local bin. |
| 24 | +* Create a start.csx and paste the code below |
| 25 | + |
| 26 | +```csharp |
| 27 | +public class TestController : System.Web.Http.ApiController { |
| 28 | + public string Get() { |
| 29 | + return "Hello world!"; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +var webApi = Require<WebApi>(); |
| 34 | +var server = webApi.CreateServer("http://localhost:8080"); |
| 35 | +server.OpenAsync().Wait(); |
| 36 | + |
| 37 | +Console.WriteLine("Listening..."); |
| 38 | +Console.ReadKey(); |
| 39 | +``` |
| 40 | +* Running as admin type ```scriptcs start.csx``` to launch the app. |
| 41 | +* Open a browser to "http://localhost:8080/test"; |
| 42 | +* That's it, your API is up! |
| 43 | + |
| 44 | +## Customizing |
| 45 | +You can customize the host by modifying the configuration object. Or if you would like to pass your own you can use the CreateServer overload. |
| 46 | +If you pass your own, the ControllerHttpResolver will be replaced with a script friendly one. |
| 47 | + |
| 48 | +## What's next |
| 49 | +* Create a nuget package. |
0 commit comments