Permalink
Browse files

Chapter 6

  • Loading branch information...
1 parent aaee3d3 commit 93c4b755332f427e72596abfdf89cbfae4792d27 @jchadwick jchadwick committed Oct 4, 2012
View
@@ -2,6 +2,8 @@ _ReSharper.*
**.ReSharper
bin
obj
+*.mdf
+*.ldf
**.user
**.suo
**.cache
@@ -0,0 +1,27 @@
+using System.Web.Mvc;
+
+namespace Ebuy
+{
+ public class JsonpResult : JsonResult
+ {
+ public string Callback { get; set; }
+
+ public JsonpResult()
+ {
+ JsonRequestBehavior = JsonRequestBehavior.AllowGet;
+ }
+
+ public override void ExecuteResult(ControllerContext context)
+ {
+ var httpContext = context.HttpContext;
+ var callback = Callback;
+
+ if (string.IsNullOrWhiteSpace(callback))
+ callback = httpContext.Request["callback"];
+
+ httpContext.Response.Write(callback + "(");
+ base.ExecuteResult(context);
+ httpContext.Response.Write(");");
+ }
+ }
+}
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{3FFE7A82-7E7D-42EB-A439-DEAACBCFBFC6}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Ebuy</RootNamespace>
+ <AssemblyName>Ebuy.Mvc</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Web" />
+ <Reference Include="System.Web.Extensions" />
+ <Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="ActionResults\JsonpActionResult.cs" />
+ <Compile Include="Extensions\JsonRequestExtensions.cs" />
+ <Compile Include="Filters\MultipleResponseFormatsAttribute.cs" />
+ <Compile Include="ModelBinders\JsonModelBinder.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,5 @@
+<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+ <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=ActionResults/@EntryIndexedValue">True</s:Boolean>
+ <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Extensions/@EntryIndexedValue">True</s:Boolean>
+ <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Filters/@EntryIndexedValue">True</s:Boolean>
+ <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=ModelBinders/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
@@ -0,0 +1,12 @@
+using System.Web;
+
+namespace Ebuy
+{
+ public static class JsonRequestExtensions
+ {
+ public static bool IsJsonRequest(this HttpRequestBase request)
+ {
+ return string.Equals(request["format"], "json");
+ }
+ }
+}
@@ -0,0 +1,36 @@
+using System.Web.Mvc;
+
+namespace Ebuy
+{
+ public class MultipleResponseFormatsAttribute : ActionFilterAttribute
+ {
+ public override void OnActionExecuted(ActionExecutedContext filterContext)
+ {
+ var request = filterContext.HttpContext.Request;
+ var viewResult = filterContext.Result as ViewResult;
+
+ if (viewResult == null)
+ return;
+
+ if (request.IsAjaxRequest())
+ {
+ // Replace result with PartialViewResult
+ filterContext.Result = new PartialViewResult
+ {
+ TempData = viewResult.TempData,
+ ViewData = viewResult.ViewData,
+ ViewName = viewResult.ViewName,
+ };
+ }
+ else if (request.IsJsonRequest())
+ {
+ // Replace result with JsonResult
+ filterContext.Result = new JsonResult
+ {
+ Data = viewResult.Model,
+ JsonRequestBehavior = JsonRequestBehavior.AllowGet
+ };
+ }
+ }
+ }
+}
@@ -0,0 +1,43 @@
+using System;
+using System.Text.RegularExpressions;
+using System.Web.Mvc;
+using System.Web.Script.Serialization;
+
+namespace Ebuy
+{
+ public class JsonModelBinder : DefaultModelBinder
+ {
+ public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
+ {
+ string json = string.Empty;
+
+ var provider = bindingContext.ValueProvider;
+
+ var providerValue = provider.GetValue(bindingContext.ModelName);
+
+ if (providerValue != null)
+ json = providerValue.AttemptedValue;
+
+ // Basic expression to make sure the string starts and ends
+ // with JSON object ( {} ) or array ( [] ) characters
+ if (Regex.IsMatch(json, @"^(\[.*\]|{.*})$"))
+ {
+ return new JavaScriptSerializer().Deserialize(json, bindingContext.ModelType);
+ }
+
+ return base.BindModel(controllerContext, bindingContext);
+ }
+ }
+
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum |
+ AttributeTargets.Interface | AttributeTargets.Parameter |
+ AttributeTargets.Struct | AttributeTargets.Property,
+ AllowMultiple = false, Inherited = false)]
+ public class JsonModelBinderAttribute : CustomModelBinderAttribute
+ {
+ public override IModelBinder GetBinder()
+ {
+ return new JsonModelBinder();
+ }
+ }
+}
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Ebuy.Mvc")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Ebuy.Mvc")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("17f2fdff-e4de-4c9e-a037-589c68177740")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -1,14 +1,17 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0" encoding="utf-8"?>
<!--
Note: Add entries to the App.config file for configuration settings
that apply only to the Test project.
-->
<configuration>
- <appSettings>
-
- </appSettings>
-
- <connectionStrings>
-
- </connectionStrings>
-</configuration>
+ <appSettings></appSettings>
+ <connectionStrings></connectionStrings>
+ <runtime>
+ <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+ <dependentAssembly>
+ <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+ <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
+ </dependentAssembly>
+ </assemblyBinding>
+ </runtime>
+</configuration>
@@ -1,21 +1,54 @@
using System;
+using System.Linq;
using System.Web.Mvc;
using Ebuy.Website.Models;
namespace Ebuy.Website.Controllers
{
public class AuctionsController : Controller
{
- //
- // GET: /Auctions/
+ [MultipleResponseFormats]
+ public ActionResult Index(int page = 0, int size = 25)
+ {
+ var db = new EbuyDataContext();
+ var auctions = db.Auctions.OrderByDescending(x => x.EndTime).Skip(page * 25).Take(size);
+ return View("Auctions", auctions);
+ }
- public ActionResult Index()
+ [MultipleResponseFormats]
+ public ActionResult Auction(long id)
{
- return View();
+ var db = new EbuyDataContext();
+ var auction = db.Auctions.Find(id);
+
+ // The following moved to MultipleResponseFormatsAttribute:
+ /*
+ // Respond to AJAX requests
+ if (Request.IsAjaxRequest())
+ return PartialView("Auction", auction);
+
+ // Respond to JSON requests
+ if (Request.IsJsonRequest())
+ return Json(auction);
+ */
+
+ // Default to a "normal" view with layout
+ return View("Auction", auction);
}
- //
- // GET: /Auctions/Details/5
+ public ActionResult JsonAuction(long id)
+ {
+ var db = new EbuyDataContext();
+ var auction = db.Auctions.Find(id);
+ return Json(auction, JsonRequestBehavior.AllowGet);
+ }
+
+ public ActionResult PartialAuction(long id)
+ {
+ var db = new EbuyDataContext();
+ var auction = db.Auctions.Find(id);
+ return PartialView("Auction", auction);
+ }
public ActionResult Details(long id = 0)
{
@@ -244,6 +244,7 @@
<Content Include="Scripts\knockout-2.1.0.debug.js" />
<Content Include="Scripts\knockout-2.1.0.js" />
<Content Include="Scripts\modernizr-2.5.3.js" />
+ <Content Include="Scripts\mustache.js" />
<Content Include="Web.config" />
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
@@ -285,13 +286,21 @@
<Content Include="Views\Web.config" />
<Content Include="Views\Auctions\Details.cshtml" />
<Content Include="Views\Auctions\Create.cshtml" />
+ <Content Include="Views\Auctions\Auction.cshtml" />
+ <Content Include="Views\Auctions\Auctions.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Ebuy.Mvc\Ebuy.Mvc.csproj">
+ <Project>{3ffe7a82-7e7d-42eb-a439-deaacbcfbfc6}</Project>
+ <Name>Ebuy.Mvc</Name>
+ </ProjectReference>
+ </ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Oops, something went wrong.

0 comments on commit 93c4b75

Please sign in to comment.