Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding response extensions

  • Loading branch information...
commit 822f0acefe6a7e762347a06f9fb9b98ea07c6bba 1 parent a172d81
@aaronpowell authored
View
4 Owin.HelloWorld/Owin.HelloWorld.csproj
@@ -63,6 +63,9 @@
<HintPath>..\packages\Gate.Builder.0.3.4\lib\net40\Gate.Builder.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.4.0.8\lib\net40\Newtonsoft.Json.dll</HintPath>
+ </Reference>
<Reference Include="Owin">
<HintPath>..\packages\Owin.0.7\lib\net40\Owin.dll</HintPath>
</Reference>
@@ -75,6 +78,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DynamicDictionary.cs" />
+ <Compile Include="RouteExtensions.cs" />
<Compile Include="Routing\Routing.cs" />
<Compile Include="Routing\Middleware.cs" />
<Compile Include="Program.cs" />
View
30 Owin.HelloWorld/RouteExtensions.cs
@@ -0,0 +1,30 @@
+using Gate;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using Newtonsoft.Json.Serialization;
+
+namespace Owin.HelloWorld
+{
+ public static class RouteExtensions
+ {
+ public static void Json(this Response res, dynamic obj, bool useJavaScriptNaming = true)
+ {
+ res.ContentType = "application/json";
+ res.Status = "200 OK";
+
+ var serializer = new JsonSerializer();
+
+ if (useJavaScriptNaming)
+ serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
+
+ res.End(JObject.FromObject(obj, serializer).ToString());
+ }
+
+ public static void Text(this Response res, string text)
+ {
+ res.ContentType = "text/plain";
+ res.Status = "200 OK";
+ res.End(text);
+ }
+ }
+}
View
14 Owin.HelloWorld/Startup.cs
@@ -19,14 +19,22 @@ public static void Configuration(IAppBuilder builder)
res.ContentType = "text/plain";
res.End("Email " + req.UrlSegments.email + " is subscribed.\r\n");
})
- .Delete(@"/users/(?<id>\d{1,5})/unsubscribe/:email", (req, res) => {
+ .Delete(@"/users/(?<id>\d{1,5})/unsubscribe/:email", (req, res) =>
+ {
res.ContentType = "text/plain";
res.End("Email " + req.UrlSegments.email + " has been unsubscribed.\r\n");
})
+ .Get("/json", (req, res) =>
+ {
+ res.Json(new { FirstName = "Aaron", LastName = "Powell" });
+ })
+ .Get("/json/:name", (req, res) =>
+ {
+ res.Json(new { Name = req.UrlSegments.name });
+ })
.Get((req, res) =>
{
- res.ContentType = "text/plain";
- res.End("Well nothing else matched, that's sad :(\r\n");
+ res.Text("Well nothing else matched, that's sad :(\r\n");
})
;
}
View
1  Owin.HelloWorld/packages.config
@@ -3,5 +3,6 @@
<package id="Firefly" version="0.4" />
<package id="Gate" version="0.3.4" />
<package id="Gate.Builder" version="0.3.4" />
+ <package id="Newtonsoft.Json" version="4.0.8" />
<package id="Owin" version="0.7" />
</packages>
Please sign in to comment.
Something went wrong with that request. Please try again.