Permalink
Browse files

Updated for RC

  • Loading branch information...
1 parent 3b51113 commit cecfdf23ae33ad02b3452e10c524fa3e8ab8fb98 @RickStrahl committed Mar 24, 2012
@@ -36,43 +36,45 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
- <Reference Include="Newtonsoft.Json">
- <HintPath>..\packages\Newtonsoft.Json.4.0.8\lib\net40\Newtonsoft.Json.dll</HintPath>
+ <Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Newtonsoft.Json.4.5.1\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Data" />
<Reference Include="System.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\System.Json.4.0.20126.16343\lib\net40\System.Json.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Json.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\System.Net.Http.2.0.20126.16343\lib\net40\System.Net.Http.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\System.Net.Http.Formatting.4.0.20126.16343\lib\net40\System.Net.Http.Formatting.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\System.Net.Http.2.0.20126.16343\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.WebRequest.dll</HintPath>
</Reference>
- <Reference Include="System" />
- <Reference Include="System.Core" />
<Reference Include="System.Web.Extensions" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Web" />
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\AspNetWebApi.Core.4.0.20126.16343\lib\net40\System.Web.Http.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\System.Web.Http.Common.4.0.20126.16343\lib\net40\System.Web.Http.Common.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.Common.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
- <Private>True</Private>
- <HintPath>..\packages\AspNetWebApi.4.0.20126.16343\lib\net40\System.Web.Http.WebHost.dll</HintPath>
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.WebHost.dll</HintPath>
</Reference>
- <Reference Include="System.Drawing" />
- <Reference Include="System.Web" />
<Reference Include="System.Web.Routing" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
@@ -136,9 +138,11 @@
<Content Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
+ <Content Include="WebForm1.aspx" />
</ItemGroup>
<ItemGroup>
<Compile Include="Code\WebApi\Filters\UnhandledExceptionFilter.cs" />
+ <Compile Include="Code\WebApi\Formatters\JsonpFormatter.cs" />
<Compile Include="Code\WebApi\Formatters\JavaScriptSerializerFormatter.cs" />
<Compile Include="Code\WebApi\Formatters\JsonNetFormatter.cs" />
<Compile Include="Controllers\AlbumApiController.cs" />
@@ -147,9 +151,18 @@
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="WebForm1.aspx.cs">
+ <DependentUpon>WebForm1.aspx</DependentUpon>
+ <SubType>ASPXCodeBehind</SubType>
+ </Compile>
+ <Compile Include="WebForm1.aspx.designer.cs">
+ <DependentUpon>WebForm1.aspx</DependentUpon>
+ </Compile>
</ItemGroup>
<ItemGroup>
- <Content Include="packages.config" />
+ <Content Include="packages.config">
+ <SubType>Designer</SubType>
+ </Content>
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
@@ -1,36 +1,38 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Web;
using System.Web.Http.Filters;
-using System.Net.Http;
using AspNetWebApi.Controllers;
using System.Net;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Net.Http.Formatting;
+using System.Web.Http;
+
+#if true
namespace AspNetWebApi
{
-public class UnhandledExceptionFilter : ExceptionFilterAttribute
-{
- public override void OnException(HttpActionExecutedContext
- context)
- {
- HttpStatusCode status = HttpStatusCode.InternalServerError;
+ public class UnhandledExceptionFilter : ExceptionFilterAttribute
+ {
+ public override void OnException(HttpActionExecutedContext
+ context)
+ {
+ HttpStatusCode status = HttpStatusCode.InternalServerError;
+
+ var exType = context.Exception.GetType();
- var exType = context.Exception.GetType();
+ if (exType == typeof(UnauthorizedAccessException))
+ status = HttpStatusCode.Unauthorized;
+ else if (exType == typeof(ArgumentException))
+ status = HttpStatusCode.NotFound;
- if (exType == typeof(UnauthorizedAccessException))
- status = HttpStatusCode.Unauthorized;
- else if (exType == typeof(ArgumentException))
- status = HttpStatusCode.NotFound;
+ var apiError = new ApiMessageError() { message = context.Exception.Message };
- var apiError = new ApiMessageError()
- { message = context.Exception.Message };
- var errMsg = new HttpResponseMessage<ApiMessageError>(apiError,
- status);
+ var errMsg = context.Request.CreateResponse<ApiMessageError>(status,apiError);
- context.Result = errMsg;
- base.OnException(context);
+ context.Result = errMsg;
+ base.OnException(context);
+ }
}
}
-}
+#endif
@@ -5,6 +5,7 @@
using System.Json;
using System.IO;
using System.Collections.Generic;
+using System.Net.Http.Headers;
namespace Westwind.Web.WebApi
{
@@ -15,7 +16,7 @@ public JavaScriptSerializerFormatter()
SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
}
- protected override bool CanWriteType(Type type)
+ public override bool CanWriteType(Type type)
{
// don't serialize JsonValue structure use default for that
if (type == typeof(JsonValue) || type == typeof(JsonObject) || type== typeof(JsonArray) )
@@ -26,18 +27,20 @@ protected override bool CanWriteType(Type type)
return true;
}
- protected override bool CanReadType(Type type)
+ public override bool CanReadType(Type type)
{
if (type == typeof(IKeyValueModel))
return false;
return true;
}
- protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, FormatterContext formatterContext)
+ public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream,
+ HttpContentHeaders contentHeaders,
+ IFormatterLogger formatterLogger)
{
var task = Task<object>.Factory.StartNew(() =>
- {
+ {
var ser = new JavaScriptSerializer();
string json;
@@ -54,7 +57,11 @@ protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Typ
return task;
}
- protected override System.Threading.Tasks.Task OnWriteToStreamAsync(Type type, object value, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, FormatterContext formatterContext, System.Net.TransportContext transportContext)
+
+ public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value,
+ System.IO.Stream stream,
+ HttpContentHeaders contentHeaders,
+ System.Net.TransportContext transportContext)
{
var task = Task.Factory.StartNew( () =>
{
@@ -1,5 +1,4 @@
// this code requires a reference to JSON.NET in your project
-#if true
using System;
using System.Net.Http.Formatting;
@@ -9,6 +8,7 @@
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Converters;
+using System.Net.Http.Headers;
namespace Westwind.Web.WebApi
{
@@ -19,7 +19,7 @@ public JsonNetFormatter()
SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
}
- protected override bool CanWriteType(Type type)
+ public override bool CanWriteType(Type type)
{
// don't serialize JsonValue structure use default for that
if (type == typeof(JsonValue) || type == typeof(JsonObject) || type == typeof(JsonArray))
@@ -28,15 +28,17 @@ protected override bool CanWriteType(Type type)
return true;
}
- protected override bool CanReadType(Type type)
+ public override bool CanReadType(Type type)
{
if (type == typeof(IKeyValueModel))
return false;
return true;
}
- protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, FormatterContext formatterContext)
+ public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type,
+ System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders,
+ IFormatterLogger formatterLogger)
{
var task = Task<object>.Factory.StartNew(() =>
{
@@ -58,7 +60,10 @@ protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Typ
return task;
}
- protected override System.Threading.Tasks.Task OnWriteToStreamAsync(Type type, object value, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, FormatterContext formatterContext, System.Net.TransportContext transportContext)
+ public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value,
+ System.IO.Stream stream,
+ HttpContentHeaders contentHeaders,
+ System.Net.TransportContext transportContext)
{
var task = Task.Factory.StartNew( () =>
{
@@ -78,5 +83,4 @@ protected override System.Threading.Tasks.Task OnWriteToStreamAsync(Type type, o
return task;
}
}
-}
-#endif
+}
@@ -0,0 +1,111 @@
+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 JsonpFormatter : JsonMediaTypeFormatter
+ {
+
+ public JsonpFormatter()
+ {
+ SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
+ SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));
+
+ //MediaTypeMappings.Add(new UriPathExtensionMapping("jsonp", "application/json"));
+
+ JsonpParameterName = "callback";
+ }
+
+ /// <summary>
+ /// Name of the query string parameter to look for
+ /// the jsonp function name
+ /// </summary>
+ public string JsonpParameterName {get; set; }
+
+ /// <summary>
+ /// Captured name of the Jsonp function that the JSON call
+ /// is wrapped in. Set in GetPerRequestFormatter Instance
+ /// </summary>
+ private string JsonpCallbackFunction;
+
+
+ public override bool CanWriteType(Type type)
+ {
+ return true;
+ }
+
+ /// <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 JsonpFormatter()
+ {
+ JsonpCallbackFunction = GetJsonCallbackFunction(request)
+ };
+
+
+ return formatter;
+ }
+
+ public override Task WriteToStreamAsync(Type type, object value, Stream stream,
+ HttpContentHeaders contentHeaders,
+ TransportContext transportContext)
+ {
+
+
+ if (!string.IsNullOrEmpty(JsonpCallbackFunction))
+ {
+ return Task.Factory.StartNew(() =>
+ {
+ var writer = new StreamWriter(stream);
+ writer.Write( JsonpCallbackFunction + "(");
+ writer.Flush();
+
+ base.WriteToStreamAsync(type, value, stream, contentHeaders,
+ transportContext).Wait();
+
+ writer.Write(")");
+ writer.Flush();
+ });
+ }
+ else
+ {
+ return base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext);
+ }
+ }
+
+ /// <summary>
+ /// Retrieves the Jsonp Callback function
+ /// from the query string
+ /// </summary>
+ /// <returns></returns>
+ private string GetJsonCallbackFunction(HttpRequestMessage request)
+ {
+ if (request.Method != HttpMethod.Get)
+ return null;
+
+ var query = HttpUtility.ParseQueryString(request.RequestUri.Query);
+ var queryVal = query[this.JsonpParameterName];
+
+ if (string.IsNullOrEmpty(queryVal) == null)
+ return null;
+
+ return queryVal;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit cecfdf2

Please sign in to comment.