Moving View overloads to Controller base type
This commit is contained in:
parent
c734eada6f
commit
69a99e5c41
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -50,8 +50,7 @@ namespace MvcSample
|
|||
|
||||
public IActionResult MyView()
|
||||
{
|
||||
ViewData.Model = User();
|
||||
return Result.View(ViewData);
|
||||
return View(User());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
@{
|
||||
@model MvcSample.User
|
||||
@{
|
||||
Layout = "~/Views/Shared/_Layout.cshtml";
|
||||
ViewData.Title = "Home Page";
|
||||
ViewBag.Title = "Home Page";
|
||||
}
|
||||
|
||||
<div class="jumbotron">
|
||||
|
|
@ -10,6 +11,7 @@
|
|||
</div>
|
||||
|
||||
<div class="row">
|
||||
<h3>Hello @Model.Name!</h3>
|
||||
<div class="col-md-4">
|
||||
<h2>Getting started</h2>
|
||||
<p>
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<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" />
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
|
|
@ -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" : { }
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -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": {}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -54,7 +54,6 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="HtmlString.cs" />
|
||||
<Compile Include="RazorViewOfT.cs" />
|
||||
<Compile Include="Razor\MvcCSharpRazorCodeGenerator.cs" />
|
||||
<Compile Include="Razor\MvcCSharpRazorCodeParser.cs" />
|
||||
|
|
@ -71,6 +70,10 @@
|
|||
<Compile Include="ViewEngine\VirtualPathViewFactory.cs" />
|
||||
</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">
|
||||
<Project>{ec38534c-a2d1-413f-97d1-55eef5d2fb71}</Project>
|
||||
<Name>Microsoft.AspNet.CoreServices</Name>
|
||||
|
|
|
|||
|
|
@ -42,15 +42,15 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
private async Task RenderLayoutAsync(ViewContext context, TextWriter writer, string bodyContent)
|
||||
{
|
||||
var virtualPathFactory = context.ServiceProvider.GetService<IVirtualPathViewFactory>();
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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<TModel> ViewData { get; set; }
|
||||
|
||||
public HtmlHelper<TModel> Html { get; set; }
|
||||
|
||||
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;
|
||||
InitHelpers(context);
|
||||
|
||||
return base.RenderAsync(context, writer);
|
||||
}
|
||||
|
||||
private void InitHelpers(RequestContext context)
|
||||
{
|
||||
Html = new HtmlHelper<TModel>(context, ViewData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<object>();
|
||||
}
|
||||
|
||||
public IActionResultHelper Result { get; private 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,6 @@
|
|||
<Compile Include="ActionInvokerProvider.cs" />
|
||||
<Compile Include="ActionResultFactory.cs" />
|
||||
<Compile Include="ActionResultHelper.cs" />
|
||||
<Compile Include="ActionResultHelperExtensions.cs" />
|
||||
<Compile Include="ActionResults\NoContentResult.cs" />
|
||||
<Compile Include="ActionResults\NegotiatedContentResult.cs" />
|
||||
<Compile Include="ActionResults\ContentResult.cs" />
|
||||
|
|
@ -89,7 +88,8 @@
|
|||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Routing\IRouteData.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\ViewResult.cs" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,21 +4,41 @@ using System.Dynamic;
|
|||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ViewDataDictionary : DynamicObject
|
||||
public class ViewData : DynamicObject
|
||||
{
|
||||
private Dictionary<object, dynamic> _data;
|
||||
|
||||
public ViewDataDictionary()
|
||||
public ViewData()
|
||||
{
|
||||
_data = new Dictionary<object, dynamic>();
|
||||
}
|
||||
|
||||
public ViewDataDictionary(ViewDataDictionary source)
|
||||
public ViewData(ViewData source)
|
||||
{
|
||||
_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)
|
||||
{
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue