From 69a99e5c4154ef37808e47cd2ddb16f3da199822 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 21 Jan 2014 16:37:50 -0800 Subject: [PATCH] Moving View overloads to Controller base type --- Microsoft.AspNet.Mvc.sln | 11 ++++- samples/MvcSample/HomeController.cs | 3 +- samples/MvcSample/Views/Home/MyView.cshtml | 6 ++- samples/MvcSample/Views/Shared/_Layout.cshtml | 2 +- samples/MvcSample/project.json | 23 +++++------ .../project.json | 14 +++---- src/Microsoft.AspNet.Mvc.Razor/HtmlString.cs | 18 --------- .../Microsoft.AspNet.Mvc.Razor.csproj | 5 ++- src/Microsoft.AspNet.Mvc.Razor/RazorView.cs | 8 ++-- .../RazorViewOfT.cs | 19 ++++++++- .../ActionResultHelper.cs | 2 +- .../ActionResultHelperExtensions.cs | 21 ---------- src/Microsoft.AspNet.Mvc/Controller.cs | 39 +++++++++++++++--- .../IActionResultHelper.cs | 2 +- .../Microsoft.AspNet.Mvc.csproj | 4 +- src/Microsoft.AspNet.Mvc/View/ViewContext.cs | 4 +- .../{ViewDataDictionary.cs => ViewData.cs} | 40 ++++++++++++------- .../View/ViewDataOfTModel.cs | 24 +++++++++++ src/Microsoft.AspNet.Mvc/View/ViewResult.cs | 2 +- 19 files changed, 149 insertions(+), 98 deletions(-) delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/HtmlString.cs delete mode 100644 src/Microsoft.AspNet.Mvc/ActionResultHelperExtensions.cs rename src/Microsoft.AspNet.Mvc/View/{ViewDataDictionary.cs => ViewData.cs} (73%) create mode 100644 src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs diff --git a/Microsoft.AspNet.Mvc.sln b/Microsoft.AspNet.Mvc.sln index 2d0fa26ece..fb0ef29ec8 100644 --- a/Microsoft.AspNet.Mvc.sln +++ b/Microsoft.AspNet.Mvc.sln @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{32285FA4-6B46-4D6B-A840-2B13E4C8B58E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Forms", "src\Microsoft.AspNet.Mvc.Forms\Microsoft.AspNet.Mvc.Forms.csproj", "{AB4CDC03-176C-460F-8955-4202F6D53FED}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,14 +39,19 @@ Global {224A14D0-ECA7-441C-AE89-B6E66A57EF9B}.Debug|Any CPU.Build.0 = Debug|Any CPU {224A14D0-ECA7-441C-AE89-B6E66A57EF9B}.Release|Any CPU.ActiveCfg = Release|Any CPU {224A14D0-ECA7-441C-AE89-B6E66A57EF9B}.Release|Any CPU.Build.0 = Release|Any CPU + {AB4CDC03-176C-460F-8955-4202F6D53FED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB4CDC03-176C-460F-8955-4202F6D53FED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB4CDC03-176C-460F-8955-4202F6D53FED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB4CDC03-176C-460F-8955-4202F6D53FED}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {069EA0A1-BB68-41D1-A973-3429EC09264C} = {DAAE4C74-D06F-4874-A166-33305D2643CE} {EC38534C-A2D1-413F-97D1-55EEF5D2FB71} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} - {2A0C26F1-0240-4AE1-AE00-4691C291B122} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} {224A14D0-ECA7-441C-AE89-B6E66A57EF9B} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} + {2A0C26F1-0240-4AE1-AE00-4691C291B122} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} + {AB4CDC03-176C-460F-8955-4202F6D53FED} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} + {069EA0A1-BB68-41D1-A973-3429EC09264C} = {DAAE4C74-D06F-4874-A166-33305D2643CE} EndGlobalSection EndGlobal diff --git a/samples/MvcSample/HomeController.cs b/samples/MvcSample/HomeController.cs index 3e0ec9789e..1af68b0d62 100644 --- a/samples/MvcSample/HomeController.cs +++ b/samples/MvcSample/HomeController.cs @@ -50,8 +50,7 @@ namespace MvcSample public IActionResult MyView() { - ViewData.Model = User(); - return Result.View(ViewData); + return View(User()); } } } \ No newline at end of file diff --git a/samples/MvcSample/Views/Home/MyView.cshtml b/samples/MvcSample/Views/Home/MyView.cshtml index 97fc4cc938..e61e4175d1 100644 --- a/samples/MvcSample/Views/Home/MyView.cshtml +++ b/samples/MvcSample/Views/Home/MyView.cshtml @@ -1,6 +1,7 @@ -@{ +@model MvcSample.User +@{ Layout = "~/Views/Shared/_Layout.cshtml"; - ViewData.Title = "Home Page"; + ViewBag.Title = "Home Page"; }
@@ -10,6 +11,7 @@
+

Hello @Model.Name!

Getting started

diff --git a/samples/MvcSample/Views/Shared/_Layout.cshtml b/samples/MvcSample/Views/Shared/_Layout.cshtml index a9ee6506c1..6b9dd0672a 100644 --- a/samples/MvcSample/Views/Shared/_Layout.cshtml +++ b/samples/MvcSample/Views/Shared/_Layout.cshtml @@ -3,7 +3,7 @@ - @ViewData.Title - My ASP.NET Application + @ViewBag.Title - My ASP.NET Application diff --git a/samples/MvcSample/project.json b/samples/MvcSample/project.json index 8ec4b1ed49..4e7b50bf0f 100644 --- a/samples/MvcSample/project.json +++ b/samples/MvcSample/project.json @@ -1,15 +1,14 @@ { - "dependencies": [ - { "Owin": { "version": "1.0" } }, - { "Microsoft.Owin.Diagnostics": { "version": "2.0.2" } }, - { "Microsoft.Owin": { "version": "2.0.2" } }, - { "Microsoft.AspNet.Mvc": { "version": "" } }, - ], - "configurations": [ - { "net45": { - "dependencies": [ - { "System.Net.Http" : { } } - ] } + "dependencies": { + "Owin": { "version": "1.0" }, + "Microsoft.Owin.Diagnostics": { "version": "2.0.2" }, + "Microsoft.Owin": { "version": "2.0.2" }, + "Microsoft.AspNet.Mvc": { "version": "" }, + }, + "configurations": { + "net45": { + "dependencies": { + "System.Net.Http" : { } + } } - ] } \ No newline at end of file diff --git a/src/Microsoft.AspNet.CoreServices/project.json b/src/Microsoft.AspNet.CoreServices/project.json index b3c09afd7e..d8a96bf94c 100644 --- a/src/Microsoft.AspNet.CoreServices/project.json +++ b/src/Microsoft.AspNet.CoreServices/project.json @@ -1,9 +1,9 @@ { - "dependencies": [ - { "Microsoft.Owin.FileSystems": { "version": "2.0.2" } } - ], - "configurations": [ - { "net45": {} }, - { "k10": {} } - ] + "dependencies": { + "Microsoft.Owin.FileSystems": "2.0.2" + }, + "configurations": { + "net45": {}, + "k10": {} + } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/HtmlString.cs b/src/Microsoft.AspNet.Mvc.Razor/HtmlString.cs deleted file mode 100644 index 33ab128651..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/HtmlString.cs +++ /dev/null @@ -1,18 +0,0 @@ - -namespace Microsoft.AspNet.Mvc.Razor -{ - public class HtmlString - { - private readonly string _input; - - public HtmlString(string input) - { - _input = input; - } - - public override string ToString() - { - return _input; - } - } -} diff --git a/src/Microsoft.AspNet.Mvc.Razor/Microsoft.AspNet.Mvc.Razor.csproj b/src/Microsoft.AspNet.Mvc.Razor/Microsoft.AspNet.Mvc.Razor.csproj index 114c214bb3..f78349c8da 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Microsoft.AspNet.Mvc.Razor.csproj +++ b/src/Microsoft.AspNet.Mvc.Razor/Microsoft.AspNet.Mvc.Razor.csproj @@ -54,7 +54,6 @@ - @@ -71,6 +70,10 @@ + + {ab4cdc03-176c-460f-8955-4202f6d53fed} + Microsoft.AspNet.Mvc.Html + {ec38534c-a2d1-413f-97d1-55eef5d2fb71} Microsoft.AspNet.CoreServices diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index 818cc72d38..f8bbee9c8f 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -42,15 +42,15 @@ namespace Microsoft.AspNet.Mvc.Razor private async Task RenderLayoutAsync(ViewContext context, TextWriter writer, string bodyContent) { var virtualPathFactory = context.ServiceProvider.GetService(); - RazorView razorView = (RazorView)(await virtualPathFactory.CreateInstance(Layout)); - if (razorView == null) + RazorView layoutView = (RazorView)(await virtualPathFactory.CreateInstance(Layout)); + if (layoutView == null) { string message = String.Format(CultureInfo.CurrentCulture, "The layout view '{0}' could not be located.", Layout); throw new InvalidOperationException(message); } - razorView.BodyContent = bodyContent; - await razorView.RenderAsync(context, writer); + layoutView.BodyContent = bodyContent; + await layoutView.RenderAsync(context, writer); } public abstract void Execute(); diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs index ef6b19584d..a1cab2a2d3 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs @@ -7,13 +7,28 @@ namespace Microsoft.AspNet.Mvc.Razor { public TModel Model { get; set; } - public dynamic ViewData { get; set; } + public dynamic ViewBag + { + get { return ViewData; } + } + + public ViewData ViewData { get; set; } + + public HtmlHelper Html { get; set; } public override Task RenderAsync(ViewContext context, TextWriter writer) { - ViewData = context.ViewData; + var viewData = context.ViewData as ViewData; + ViewData = viewData ?? new ViewData(context.ViewData); Model = (TModel)ViewData.Model; + InitHelpers(context); + return base.RenderAsync(context, writer); } + + private void InitHelpers(RequestContext context) + { + Html = new HtmlHelper(context, ViewData); + } } } diff --git a/src/Microsoft.AspNet.Mvc/ActionResultHelper.cs b/src/Microsoft.AspNet.Mvc/ActionResultHelper.cs index 79677eb59b..76d05ad996 100644 --- a/src/Microsoft.AspNet.Mvc/ActionResultHelper.cs +++ b/src/Microsoft.AspNet.Mvc/ActionResultHelper.cs @@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Mvc throw new NotImplementedException(); } - public IActionResult View(string view, ViewDataDictionary viewData) + public IActionResult View(string view, ViewData viewData) { return new ViewResult(_serviceProvider, _viewEngine) { diff --git a/src/Microsoft.AspNet.Mvc/ActionResultHelperExtensions.cs b/src/Microsoft.AspNet.Mvc/ActionResultHelperExtensions.cs deleted file mode 100644 index 6c7a32d36c..0000000000 --- a/src/Microsoft.AspNet.Mvc/ActionResultHelperExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ - -namespace Microsoft.AspNet.Mvc -{ - public static class ActionResultHelperExtensions - { - public static IActionResult View(this IActionResultHelper actionResultHelper) - { - return actionResultHelper.View(view: null, viewData: null); - } - - public static IActionResult View(this IActionResultHelper actionResultHelper, string view) - { - return actionResultHelper.View(view, viewData: null); - } - - public static IActionResult View(this IActionResultHelper actionResultHelper, ViewDataDictionary viewData) - { - return actionResultHelper.View(view: null, viewData: viewData); - } - } -} diff --git a/src/Microsoft.AspNet.Mvc/Controller.cs b/src/Microsoft.AspNet.Mvc/Controller.cs index d6af014875..dfcf3be57b 100644 --- a/src/Microsoft.AspNet.Mvc/Controller.cs +++ b/src/Microsoft.AspNet.Mvc/Controller.cs @@ -1,20 +1,49 @@ -using System.Threading.Tasks; -using Microsoft.Owin; +using Microsoft.Owin; namespace Microsoft.AspNet.Mvc { public class Controller - { + { public void Initialize(IActionResultHelper actionResultHelper) { Result = actionResultHelper; - ViewData = new ViewDataDictionary(); + ViewData = new ViewData(); } public IActionResultHelper Result { get; private set; } public IOwinContext Context { get; set; } - public ViewDataDictionary ViewData { get; private set; } + public ViewData ViewData { get; set; } + + public dynamic ViewBag + { + get { return ViewData; } + } + + public IActionResult View() + { + return View(view: null); + } + + public IActionResult View(string view) + { + object model = null; + return View(view, model); + } + + public IActionResult View(TModel model) + { + return View(view: null, model: model); + } + + public IActionResult View(string view, TModel model) + { + var viewDataDictionary = new ViewData + { + Model = model + }; + return Result.View(view, viewDataDictionary); + } } } diff --git a/src/Microsoft.AspNet.Mvc/IActionResultHelper.cs b/src/Microsoft.AspNet.Mvc/IActionResultHelper.cs index be8c7edbdb..19c64cb90e 100644 --- a/src/Microsoft.AspNet.Mvc/IActionResultHelper.cs +++ b/src/Microsoft.AspNet.Mvc/IActionResultHelper.cs @@ -6,6 +6,6 @@ namespace Microsoft.AspNet.Mvc IActionResult Content(string value); IActionResult Content(string value, string contentType); IActionResult Json(object value); - IActionResult View(string view, ViewDataDictionary viewData); + IActionResult View(string view, ViewData viewData); } } diff --git a/src/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj b/src/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj index 35d60e3ec6..b73eac90a9 100644 --- a/src/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj +++ b/src/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj @@ -53,7 +53,6 @@ - @@ -89,7 +88,8 @@ - + + diff --git a/src/Microsoft.AspNet.Mvc/View/ViewContext.cs b/src/Microsoft.AspNet.Mvc/View/ViewContext.cs index f9cd20cc05..21ef8c8f49 100644 --- a/src/Microsoft.AspNet.Mvc/View/ViewContext.cs +++ b/src/Microsoft.AspNet.Mvc/View/ViewContext.cs @@ -6,7 +6,7 @@ namespace Microsoft.AspNet.Mvc { public class ViewContext : RequestContext { - public ViewContext(IOwinContext context, IRouteData routeData, ViewDataDictionary viewData) : + public ViewContext(IOwinContext context, IRouteData routeData, ViewData viewData) : base(context, routeData) { ViewData = viewData; @@ -14,6 +14,6 @@ namespace Microsoft.AspNet.Mvc public IServiceProvider ServiceProvider { get; set; } - public ViewDataDictionary ViewData { get; private set; } + public ViewData ViewData { get; private set; } } } diff --git a/src/Microsoft.AspNet.Mvc/View/ViewDataDictionary.cs b/src/Microsoft.AspNet.Mvc/View/ViewData.cs similarity index 73% rename from src/Microsoft.AspNet.Mvc/View/ViewDataDictionary.cs rename to src/Microsoft.AspNet.Mvc/View/ViewData.cs index 11fa9a2126..9e36112701 100644 --- a/src/Microsoft.AspNet.Mvc/View/ViewDataDictionary.cs +++ b/src/Microsoft.AspNet.Mvc/View/ViewData.cs @@ -4,21 +4,41 @@ using System.Dynamic; namespace Microsoft.AspNet.Mvc { - public class ViewDataDictionary : DynamicObject + public class ViewData : DynamicObject { private Dictionary _data; - public ViewDataDictionary() + public ViewData() { _data = new Dictionary(); } - public ViewDataDictionary(ViewDataDictionary source) + public ViewData(ViewData source) { _data = new Dictionary(source._data); } - public object Model { get; set; } + public dynamic this[string index] + { + get + { + dynamic result; + if (_data.TryGetValue(index, out result)) + { + result = _data[index]; + } + else + { + result = null; + } + + return result; + } + set + { + _data[index] = (dynamic)value; + } + } public override bool TryGetMember(GetMemberBinder binder, out object result) { @@ -44,15 +64,7 @@ namespace Microsoft.AspNet.Mvc } object index = indexes[0]; - - if (_data.TryGetValue(index, out result)) - { - result = _data[index]; - } - else - { - result = null; - } + result = this[(string)index]; return true; } @@ -65,7 +77,7 @@ namespace Microsoft.AspNet.Mvc object index = indexes[0]; // This cast should always succeed assuming TValue is dynamic. - _data[index] = (dynamic)value; + this[(string)index] = value; return true; } } diff --git a/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs b/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs new file mode 100644 index 0000000000..c4c3966eba --- /dev/null +++ b/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs @@ -0,0 +1,24 @@ + +namespace Microsoft.AspNet.Mvc +{ + public class ViewData : ViewData + { + public ViewData() + : base() + { + } + + public ViewData(ViewData source) : + base(source) + { + } + + public ViewData(ViewData source) + : base(source) + { + Model = source.Model; + } + + public TModel Model { get; set; } + } +} diff --git a/src/Microsoft.AspNet.Mvc/View/ViewResult.cs b/src/Microsoft.AspNet.Mvc/View/ViewResult.cs index 0c8ff8249e..588db8e9fb 100644 --- a/src/Microsoft.AspNet.Mvc/View/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc/View/ViewResult.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Mvc public string ViewName {get; set; } - public ViewDataDictionary ViewData { get; set; } + public ViewData ViewData { get; set; } public async Task ExecuteResultAsync(RequestContext context) {