Skip to content

Commit

Permalink
Few tweaks to update for RTM bits
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Aug 10, 2012
1 parent cb59510 commit ffe9506
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 13 deletions.
4 changes: 3 additions & 1 deletion AspNetWebApi/AspNetWebApi.csproj
Expand Up @@ -140,10 +140,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Code\WebApi\Filters\UnhandledExceptionFilter.cs" />
<Compile Include="Code\WebApi\Formatters\UrlEncodedHandler.cs" />
<Compile Include="Code\WebApi\Formatters\JsonpFormatter.cs" />
<Compile Include="Code\WebApi\Formatters\JavaScriptSerializerFormatter.cs" />
<Compile Include="Code\WebApi\Formatters\JsonNetFormatter.cs" />
<Compile Include="Code\WebApi\Formatters\UrlEncodedHandler.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Controllers\AlbumApiController.cs" />
<Compile Include="Controllers\AlbumRpcApiController.cs" />
<Compile Include="Code\WebApi\Filters\ApiMessageError.cs" />
Expand Down
Expand Up @@ -33,7 +33,7 @@ public override bool CanReadType(Type type)
}

public override Task<object> ReadFromStreamAsync(Type type, Stream readStream,
HttpContentHeaders contentHeaders,
HttpContent content,
IFormatterLogger formatterLogger)
{
var task = Task<object>.Factory.StartNew(() =>
Expand All @@ -56,7 +56,7 @@ public override bool CanReadType(Type type)

public override Task WriteToStreamAsync(Type type, object value,
Stream stream,
HttpContentHeaders contentHeaders,
HttpContent content,
TransportContext transportContext)
{
var task = Task.Factory.StartNew( () =>
Expand Down
4 changes: 2 additions & 2 deletions AspNetWebApi/Code/WebApi/Formatters/JsonNetFormatter.cs
Expand Up @@ -37,7 +37,7 @@ public override bool CanReadType(Type type)

public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type,
Stream stream,
HttpContentHeaders content,
HttpContent content,
IFormatterLogger formatterLogger)
{
var task = Task<object>.Factory.StartNew(() =>
Expand All @@ -62,7 +62,7 @@ public override bool CanReadType(Type type)

public override Task WriteToStreamAsync(Type type, object value,
Stream stream,
HttpContentHeaders contentHeaders,
HttpContent content,
TransportContext transportContext)
{
var task = Task.Factory.StartNew( () =>
Expand Down
6 changes: 3 additions & 3 deletions AspNetWebApi/Code/WebApi/Formatters/JsonpFormatter.cs
Expand Up @@ -64,7 +64,7 @@ public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, Sys

public override Task WriteToStreamAsync(Type type, object value,
Stream stream,
HttpContentHeaders contentHeaders,
HttpContent content,
TransportContext transportContext)
{
if (!string.IsNullOrEmpty(JsonpCallbackFunction))
Expand All @@ -76,7 +76,7 @@ public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, Sys
writer.Write( JsonpCallbackFunction + "(");
writer.Flush();
base.WriteToStreamAsync(type, value, stream, contentHeaders,
base.WriteToStreamAsync(type, value, stream, content,
transportContext).Wait();
writer.Write(")");
Expand All @@ -86,7 +86,7 @@ public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, Sys
}
else
{
return base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext);
return base.WriteToStreamAsync(type, value, stream, content, transportContext);
}
}

Expand Down
65 changes: 65 additions & 0 deletions AspNetWebApi/Code/WebApi/Formatters/UrlEncodedHandler.cs
@@ -0,0 +1,65 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http;

namespace Westwind.Web.WebApi
{

/// <summary>
/// Handles JsonP requests when requests are fired with text/javascript
/// </summary>
public class UrlEncodedFormatter : MediaTypeFormatter
{

public UrlEncodedFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-www-form-urlencoded"));

//MediaTypeMappings.Add(new UriPathExtensionMapping("jsonp", "application/json"));

}


public override bool CanWriteType(Type type)
{
return true;
}

public override bool CanReadType(Type type)
{
return false;
}

/// <summary>
/// Override this method to capture the Request object
/// </summary>
/// <param name="type"></param>
/// <param name="request"></param>
/// <param name="mediaType"></param>
/// <returns></returns>
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, MediaTypeHeaderValue mediaType)
{
var formatter = new UrlEncodedFormatter();
{
};


return formatter;
}


public override Task WriteToStreamAsync(Type type, object value,
Stream stream,
HttpContent content,
TransportContext transportContext)
{
return base.WriteToStreamAsync(type, value, stream, content, transportContext);
}

}
}
5 changes: 4 additions & 1 deletion AspNetWebApi/Controllers/AlbumRpcApiController.cs
Expand Up @@ -21,10 +21,13 @@ namespace AspNetWebApi.Controllers
/// </summary>
public class AlbumRpcApiController : ApiController
{
[HttpGet,Queryable]
[HttpGet]
public IQueryable<Album> SortableAlbums()
{
var albums = AlbumData.Current;

// generally should be done only on actual queryable results (EF etc.)
// Done here because we're running with a static list but otherwise might be slow
return albums.AsQueryable();
}

Expand Down
4 changes: 3 additions & 1 deletion AspNetWebApi/Global.asax.cs
Expand Up @@ -2,6 +2,7 @@
using System.Web.Routing;
using System.Web.Http;
using System.Globalization;
using Westwind.Web.WebApi;
//using Westwind.Web.WebApi;

namespace AspNetWebApi
Expand Down Expand Up @@ -91,7 +92,8 @@ public static void RegisterApis(HttpConfiguration config)
// This leaves the old one in place so JsonValue/JsonObject/JsonArray still are handled
//config.Formatters.Insert(0, new JsonNetFormatter());

config.Formatters.Insert(0, new Westwind.Web.WebApi.JsonpFormatter());
config.Formatters.Insert(0, new JsonpFormatter());
config.Formatters.Insert(1, new UrlEncodedFormatter());

// Add an exception filter
//GlobalConfiguration.Configuration.Filters.Add(new UnhandledExceptionFilter());
Expand Down
7 changes: 5 additions & 2 deletions AspNetWebApi/JsonpClient.htm
Expand Up @@ -11,11 +11,14 @@
<h1>JSONP Callbacks with the JsonpFormatter</h1>
<div class="descriptionheader">
This example demonstrates calling one of the Web API endpoints
by using JSONP which allows for cross-domain access to JSON data.
The configuration is adds a custom JSONP formatter (in global.asax)
by using JSONP with a custom JSONP formatter, which allows for
cross-domain access to JSON data. The configuration adds a custom JSONP formatter (in global.asax)
that provides this functionality. This example is local by default,
but if you access the albums URL remotely with a JSONP request it
will serve the data cross-domain.
(although this sample is local you can run this sample on another machine and point to that domain/url to test cross site
operation).

</div>

<div class="toolbarcontainer">
Expand Down
4 changes: 3 additions & 1 deletion AspNetWebApi/default.htm
Expand Up @@ -27,7 +27,9 @@ <h3>REST Links</h3>
<a href="albums/Dirty Deeds/image/">Album Cover Picture</a>
</li>
<li>
<a href="albums/rpc/SortableAlbums?$orderby=YearReleased&$top=3">IQueryable Results</a> with <a href="http://www.odata.org/developers/protocols/uri-conventions">OData Filtering</a>
<s><a href="albums/rpc/SortableAlbums?$orderby=YearReleased&$top=3">IQueryable Results</a> with <a href="http://www.odata.org/developers/protocols/uri-conventions">OData Filtering</a></s>
<br />
<small><i>(This functionality was removed from the RTM version of Web API and will be added back later with a separate oData package)</i></small>
</li>
</ul>

Expand Down

0 comments on commit ffe9506

Please sign in to comment.