Moving View overloads to Controller base type

This commit is contained in:
Pranav K 2014-01-21 16:37:50 -08:00
parent c734eada6f
commit 69a99e5c41
19 changed files with 149 additions and 98 deletions

View File

@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{32285FA4-6B46-4D6B-A840-2B13E4C8B58E}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{32285FA4-6B46-4D6B-A840-2B13E4C8B58E}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{224A14D0-ECA7-441C-AE89-B6E66A57EF9B}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{069EA0A1-BB68-41D1-A973-3429EC09264C} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{EC38534C-A2D1-413F-97D1-55EEF5D2FB71} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} {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} {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 EndGlobalSection
EndGlobal EndGlobal

View File

@ -50,8 +50,7 @@ namespace MvcSample
public IActionResult MyView() public IActionResult MyView()
{ {
ViewData.Model = User(); return View(User());
return Result.View(ViewData);
} }
} }
} }

View File

@ -1,6 +1,7 @@
@{ @model MvcSample.User
@{
Layout = "~/Views/Shared/_Layout.cshtml"; Layout = "~/Views/Shared/_Layout.cshtml";
ViewData.Title = "Home Page"; ViewBag.Title = "Home Page";
} }
<div class="jumbotron"> <div class="jumbotron">
@ -10,6 +11,7 @@
</div> </div>
<div class="row"> <div class="row">
<h3>Hello @Model.Name!</h3>
<div class="col-md-4"> <div class="col-md-4">
<h2>Getting started</h2> <h2>Getting started</h2>
<p> <p>

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewData.Title - My ASP.NET Application</title> <title>@ViewBag.Title - My ASP.NET Application</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" /> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
</head> </head>
<body> <body>

View File

@ -1,15 +1,14 @@
{ {
"dependencies": [ "dependencies": {
{ "Owin": { "version": "1.0" } }, "Owin": { "version": "1.0" },
{ "Microsoft.Owin.Diagnostics": { "version": "2.0.2" } }, "Microsoft.Owin.Diagnostics": { "version": "2.0.2" },
{ "Microsoft.Owin": { "version": "2.0.2" } }, "Microsoft.Owin": { "version": "2.0.2" },
{ "Microsoft.AspNet.Mvc": { "version": "" } }, "Microsoft.AspNet.Mvc": { "version": "" },
], },
"configurations": [ "configurations": {
{ "net45": { "net45": {
"dependencies": [ "dependencies": {
{ "System.Net.Http" : { } } "System.Net.Http" : { }
] } }
} }
]
} }

View File

@ -1,9 +1,9 @@
{ {
"dependencies": [ "dependencies": {
{ "Microsoft.Owin.FileSystems": { "version": "2.0.2" } } "Microsoft.Owin.FileSystems": "2.0.2"
], },
"configurations": [ "configurations": {
{ "net45": {} }, "net45": {},
{ "k10": {} } "k10": {}
] }
} }

View File

@ -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;
}
}
}

View File

@ -54,7 +54,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="HtmlString.cs" />
<Compile Include="RazorViewOfT.cs" /> <Compile Include="RazorViewOfT.cs" />
<Compile Include="Razor\MvcCSharpRazorCodeGenerator.cs" /> <Compile Include="Razor\MvcCSharpRazorCodeGenerator.cs" />
<Compile Include="Razor\MvcCSharpRazorCodeParser.cs" /> <Compile Include="Razor\MvcCSharpRazorCodeParser.cs" />
@ -71,6 +70,10 @@
<Compile Include="ViewEngine\VirtualPathViewFactory.cs" /> <Compile Include="ViewEngine\VirtualPathViewFactory.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Microsoft.AspNet.Mvc.Forms\Microsoft.AspNet.Mvc.Forms.csproj">
<Project>{ab4cdc03-176c-460f-8955-4202f6d53fed}</Project>
<Name>Microsoft.AspNet.Mvc.Html</Name>
</ProjectReference>
<ProjectReference Include="..\Microsoft.AspNet.CoreServices\Microsoft.AspNet.CoreServices.csproj"> <ProjectReference Include="..\Microsoft.AspNet.CoreServices\Microsoft.AspNet.CoreServices.csproj">
<Project>{ec38534c-a2d1-413f-97d1-55eef5d2fb71}</Project> <Project>{ec38534c-a2d1-413f-97d1-55eef5d2fb71}</Project>
<Name>Microsoft.AspNet.CoreServices</Name> <Name>Microsoft.AspNet.CoreServices</Name>

View File

@ -42,15 +42,15 @@ namespace Microsoft.AspNet.Mvc.Razor
private async Task RenderLayoutAsync(ViewContext context, TextWriter writer, string bodyContent) private async Task RenderLayoutAsync(ViewContext context, TextWriter writer, string bodyContent)
{ {
var virtualPathFactory = context.ServiceProvider.GetService<IVirtualPathViewFactory>(); var virtualPathFactory = context.ServiceProvider.GetService<IVirtualPathViewFactory>();
RazorView razorView = (RazorView)(await virtualPathFactory.CreateInstance(Layout)); RazorView layoutView = (RazorView)(await virtualPathFactory.CreateInstance(Layout));
if (razorView == null) if (layoutView == null)
{ {
string message = String.Format(CultureInfo.CurrentCulture, "The layout view '{0}' could not be located.", Layout); string message = String.Format(CultureInfo.CurrentCulture, "The layout view '{0}' could not be located.", Layout);
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
razorView.BodyContent = bodyContent; layoutView.BodyContent = bodyContent;
await razorView.RenderAsync(context, writer); await layoutView.RenderAsync(context, writer);
} }
public abstract void Execute(); public abstract void Execute();

View File

@ -7,13 +7,28 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
public TModel Model { get; set; } public TModel Model { get; set; }
public dynamic ViewData { get; set; } public dynamic ViewBag
{
get { return ViewData; }
}
public ViewData<TModel> ViewData { get; set; }
public HtmlHelper<TModel> Html { get; set; }
public override Task RenderAsync(ViewContext context, TextWriter writer) public override Task RenderAsync(ViewContext context, TextWriter writer)
{ {
ViewData = context.ViewData; var viewData = context.ViewData as ViewData<TModel>;
ViewData = viewData ?? new ViewData<TModel>(context.ViewData);
Model = (TModel)ViewData.Model; Model = (TModel)ViewData.Model;
InitHelpers(context);
return base.RenderAsync(context, writer); return base.RenderAsync(context, writer);
} }
private void InitHelpers(RequestContext context)
{
Html = new HtmlHelper<TModel>(context, ViewData);
}
} }
} }

View File

@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Mvc
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IActionResult View(string view, ViewDataDictionary viewData) public IActionResult View(string view, ViewData viewData)
{ {
return new ViewResult(_serviceProvider, _viewEngine) return new ViewResult(_serviceProvider, _viewEngine)
{ {

View File

@ -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);
}
}
}

View File

@ -1,20 +1,49 @@
using System.Threading.Tasks; using Microsoft.Owin;
using Microsoft.Owin;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
{ {
public class Controller public class Controller
{ {
public void Initialize(IActionResultHelper actionResultHelper) public void Initialize(IActionResultHelper actionResultHelper)
{ {
Result = actionResultHelper; Result = actionResultHelper;
ViewData = new ViewDataDictionary(); ViewData = new ViewData<object>();
} }
public IActionResultHelper Result { get; private set; } public IActionResultHelper Result { get; private set; }
public IOwinContext Context { get; set; } public IOwinContext Context { get; set; }
public ViewDataDictionary ViewData { get; private set; } public ViewData<object> 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>(TModel model)
{
return View(view: null, model: model);
}
public IActionResult View<TModel>(string view, TModel model)
{
var viewDataDictionary = new ViewData<TModel>
{
Model = model
};
return Result.View(view, viewDataDictionary);
}
} }
} }

View File

@ -6,6 +6,6 @@ namespace Microsoft.AspNet.Mvc
IActionResult Content(string value); IActionResult Content(string value);
IActionResult Content(string value, string contentType); IActionResult Content(string value, string contentType);
IActionResult Json(object value); IActionResult Json(object value);
IActionResult View(string view, ViewDataDictionary viewData); IActionResult View(string view, ViewData viewData);
} }
} }

View File

@ -53,7 +53,6 @@
<Compile Include="ActionInvokerProvider.cs" /> <Compile Include="ActionInvokerProvider.cs" />
<Compile Include="ActionResultFactory.cs" /> <Compile Include="ActionResultFactory.cs" />
<Compile Include="ActionResultHelper.cs" /> <Compile Include="ActionResultHelper.cs" />
<Compile Include="ActionResultHelperExtensions.cs" />
<Compile Include="ActionResults\NoContentResult.cs" /> <Compile Include="ActionResults\NoContentResult.cs" />
<Compile Include="ActionResults\NegotiatedContentResult.cs" /> <Compile Include="ActionResults\NegotiatedContentResult.cs" />
<Compile Include="ActionResults\ContentResult.cs" /> <Compile Include="ActionResults\ContentResult.cs" />
@ -89,7 +88,8 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Routing\IRouteData.cs" /> <Compile Include="Routing\IRouteData.cs" />
<Compile Include="View\ViewContext.cs" /> <Compile Include="View\ViewContext.cs" />
<Compile Include="View\ViewDataDictionary.cs" /> <Compile Include="View\ViewData.cs" />
<Compile Include="View\ViewDataOfTModel.cs" />
<Compile Include="View\ViewEngineResult.cs" /> <Compile Include="View\ViewEngineResult.cs" />
<Compile Include="View\ViewResult.cs" /> <Compile Include="View\ViewResult.cs" />
</ItemGroup> </ItemGroup>

View File

@ -6,7 +6,7 @@ namespace Microsoft.AspNet.Mvc
{ {
public class ViewContext : RequestContext public class ViewContext : RequestContext
{ {
public ViewContext(IOwinContext context, IRouteData routeData, ViewDataDictionary viewData) : public ViewContext(IOwinContext context, IRouteData routeData, ViewData viewData) :
base(context, routeData) base(context, routeData)
{ {
ViewData = viewData; ViewData = viewData;
@ -14,6 +14,6 @@ namespace Microsoft.AspNet.Mvc
public IServiceProvider ServiceProvider { get; set; } public IServiceProvider ServiceProvider { get; set; }
public ViewDataDictionary ViewData { get; private set; } public ViewData ViewData { get; private set; }
} }
} }

View File

@ -4,21 +4,41 @@ using System.Dynamic;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
{ {
public class ViewDataDictionary : DynamicObject public class ViewData : DynamicObject
{ {
private Dictionary<object, dynamic> _data; private Dictionary<object, dynamic> _data;
public ViewDataDictionary() public ViewData()
{ {
_data = new Dictionary<object, dynamic>(); _data = new Dictionary<object, dynamic>();
} }
public ViewDataDictionary(ViewDataDictionary source) public ViewData(ViewData source)
{ {
_data = new Dictionary<object, dynamic>(source._data); _data = new Dictionary<object, dynamic>(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) public override bool TryGetMember(GetMemberBinder binder, out object result)
{ {
@ -44,15 +64,7 @@ namespace Microsoft.AspNet.Mvc
} }
object index = indexes[0]; object index = indexes[0];
result = this[(string)index];
if (_data.TryGetValue(index, out result))
{
result = _data[index];
}
else
{
result = null;
}
return true; return true;
} }
@ -65,7 +77,7 @@ namespace Microsoft.AspNet.Mvc
object index = indexes[0]; object index = indexes[0];
// This cast should always succeed assuming TValue is dynamic. // This cast should always succeed assuming TValue is dynamic.
_data[index] = (dynamic)value; this[(string)index] = value;
return true; return true;
} }
} }

View File

@ -0,0 +1,24 @@

namespace Microsoft.AspNet.Mvc
{
public class ViewData<TModel> : ViewData
{
public ViewData()
: base()
{
}
public ViewData(ViewData source) :
base(source)
{
}
public ViewData(ViewData<TModel> source)
: base(source)
{
Model = source.Model;
}
public TModel Model { get; set; }
}
}

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Mvc
public string ViewName {get; set; } public string ViewName {get; set; }
public ViewDataDictionary ViewData { get; set; } public ViewData ViewData { get; set; }
public async Task ExecuteResultAsync(RequestContext context) public async Task ExecuteResultAsync(RequestContext context)
{ {