@@ -8,70 +8,53 @@ Makes using ASP.NET Web API's self host with scriptcs easy as cake, much easier
8
8
9
9
## Highlights:
10
10
11
- * Creates a pre-configured self host with the default route already added.
12
- * Configures to allow resolving script controllers.
11
+ * Creates a pre-configured OWIN self host with the default routes already added.
12
+ * Supports configuring the OWIN pipeline
13
+ * Write your controllers as scripts
13
14
* Automatically imports common web api namespaces for you.
14
- * Works as self hosted server or in OWIN
15
15
16
16
## Getting started with Web API using the pack
17
17
18
- Disclaimer: Ultimately (soon) you will be able to install this via nuget and not have to clone / build / copy
19
-
20
18
* Create a new folder for your script i.e. c:\hellowebapi and change to it.
21
- * Install the Web API script pack ``` scriptcs -install -pre ScriptCs.WebApi ```
19
+ * Install the Web API script pack ``` scriptcs -install -pre ScriptCs.WebApi2 ```
22
20
* Create a start.csx and paste the code below
23
21
24
22
``` csharp
25
- public class TestController : ApiController {
26
- public string Get () {
27
- return " Hello world!" ;
28
- }
23
+ using System .Dynamic ;
24
+
25
+ public class TestController : ApiController
26
+ {
27
+ public dynamic Get () {
28
+ dynamic obj = new ExpandoObject ();
29
+ obj .message = " Hello from Web Api" ;
30
+ return obj ;
31
+ }
29
32
}
30
33
31
- var webApi = Require <WebApi >();
32
- var server = webApi .CreateServer (" http://localhost:8080" );
33
- server .OpenAsync ().Wait ();
34
-
34
+ var webapi = Require <WebApi >();
35
+
36
+ var server = webapi .
37
+ Configure (typeof (TestController )).
38
+ UseJsonOnly ().
39
+ Start (" http://localhost:8080" );
40
+
35
41
Console .WriteLine (" Listening..." );
36
42
Console .ReadLine ();
37
- server .CloseAsync (). Wait ();
43
+ server .Dispose ();
38
44
```
39
- * Running as admin type ``` scriptcs start.csx ``` to launch the app.
45
+ * Running as admin type ``` scriptcs start.csx -modules mono ``` on Windows or ``` scriptcs start.csx ``` on Mac/Linux to launch the app.
40
46
* Open a browser to "http://localhost:8080/api/test ";
41
47
* That's it, your API is up!
42
48
43
- Alternatively you can host the script pack in OWIN e.g.
44
-
45
- ``` csharp
46
- public class TestController : ApiController {
47
- public string Get () {
48
- return " Hello world!" ;
49
- }
50
- }
51
-
52
- Require <OwinSelfHost >();
53
-
54
- var webApi = Require <WebApi >();
55
- var config = webApi .Create ();
56
-
57
- using ( OwinSelfHost .CreateServer (" http://localhost:8080" , app => app .UseWebApi (config )) ) {
58
- Console .WriteLine (" Listening..." );
59
- Console .ReadLine ();
60
- }
61
- ```
62
-
63
-
64
49
## Customizing
65
- You can customize the host by modifying the configuration object.
66
- Or if you would like to pass your own you can use the ` CreateServer ` overload.
67
- Additional ` CreateServer ` overloads allow you to explicitly specify assemblies or ` IHttpController ` types you want to expose in your api:
68
-
50
+ You can configure the OWIN host by passing in an ` Action<IBuilder> ` to the ` Configure ` method
69
51
``` csharp
70
- // Use a custom configuration and specify controller types.
71
- var config = new HttpSelfHostConfiguration (" http://localhost:8080" );
72
- var controllers = new List <Type > { typeof (TestController ) };
73
- var server = webApi .CreateServer (config , controllers )
52
+ var server = webapi .
53
+ Configure (
54
+ typeof (TestController ),
55
+ builder => {
56
+ builder .Use <MyMiddleware >();
57
+ }
58
+ ).
59
+ Start (" http://localhost:8080" );
74
60
```
75
-
76
- ## What's next
77
- TBD
0 commit comments