From 6c9d562fc39257fd770494dc5ed5e62c4e43c67c Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 30 Jan 2014 07:26:46 -0800 Subject: [PATCH] * Using RazorView as base type until we get model support * Modifying ViewData to share inner dictionary until we figure out how to pass it correctly * Removing unused NuGet.config --- NuGet.Config | 13 ------------ samples/MvcSample/Startup.cs | 10 +++------- samples/MvcSample/Views/Home/MyView.cshtml | 4 ++-- .../Compilation/CscBasedCompilationService.cs | 16 ++++++++++++--- src/Microsoft.AspNet.Mvc.Razor/RazorView.cs | 2 +- .../RazorViewOfT.cs | 2 +- .../ViewEngine/VirtualFileSystem.cs | 3 ++- .../ViewEngine/VirtualPathViewFactory.cs | 5 ++--- src/Microsoft.AspNet.Mvc/View/ViewData.cs | 20 +++++++++++++++++-- .../View/ViewDataOfTModel.cs | 12 +++++++---- 10 files changed, 50 insertions(+), 37 deletions(-) delete mode 100644 NuGet.Config diff --git a/NuGet.Config b/NuGet.Config deleted file mode 100644 index ab583b0ff7..0000000000 --- a/NuGet.Config +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/MvcSample/Startup.cs b/samples/MvcSample/Startup.cs index 020a7628ee..b8b80a1806 100644 --- a/samples/MvcSample/Startup.cs +++ b/samples/MvcSample/Startup.cs @@ -17,16 +17,12 @@ namespace MvcSample var serviceProvider = MvcServices.Create(); - // HACK to determine app root. - string appRoot = Environment.CurrentDirectory; - while (!String.IsNullOrEmpty(appRoot) && !appRoot.TrimEnd(Path.DirectorySeparatorChar).EndsWith("MvcSample")) - { - appRoot = Path.GetDirectoryName(appRoot); - } + // HACK appbase doesn't seem to work. When in VS we're pointing at bin\Debug\Net45, so move up 3 directories + string appRoot = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "..", "..", "..")); serviceProvider.AddInstance(new PhysicalFileSystem(appRoot)); serviceProvider.Add(); - serviceProvider.Add(); + serviceProvider.AddInstance(new MvcRazorHost("Microsoft.AspNet.Mvc.Razor.RazorView")); serviceProvider.Add(); serviceProvider.Add(); serviceProvider.Add(); diff --git a/samples/MvcSample/Views/Home/MyView.cshtml b/samples/MvcSample/Views/Home/MyView.cshtml index e61e4175d1..27f328ad83 100644 --- a/samples/MvcSample/Views/Home/MyView.cshtml +++ b/samples/MvcSample/Views/Home/MyView.cshtml @@ -1,4 +1,4 @@ -@model MvcSample.User + @{ Layout = "~/Views/Shared/_Layout.cshtml"; ViewBag.Title = "Home Page"; @@ -11,7 +11,7 @@
-

Hello @Model.Name!

+

Hello!

Getting started

diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs index fe60dfba84..7e6f0fabbf 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs @@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Mvc.Razor string outFile = Path.Combine(_tempDir, Path.GetRandomFileName() + ".dll"); StringBuilder args = new StringBuilder("/target:library "); args.AppendFormat("/out:\"{0}\" ", outFile); - foreach (var file in Directory.EnumerateFiles(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.dll")) + foreach (var file in Directory.EnumerateFiles(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.dll")) { args.AppendFormat("/R:\"{0}\" ", file); } @@ -37,8 +37,18 @@ namespace Microsoft.AspNet.Mvc.Razor var outputStream = new MemoryStream(); // common execute - var process = CreateProcess(args.ToString()); - int exitCode = await Start(process, outputStream); + Process process = CreateProcess(args.ToString()); + int exitCode; + try + { + File.WriteAllText(inFile, contents); + exitCode = await Start(process, outputStream); + } + finally + { + File.Delete(inFile); + } + string output = GetString(outputStream); if (exitCode != 0) diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index f57b3ef346..142fa07391 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -4,8 +4,8 @@ using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; -using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.DependencyInjection; namespace Microsoft.AspNet.Mvc.Razor { diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs index a1cab2a2d3..6c85992183 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorViewOfT.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Mvc.Razor { var viewData = context.ViewData as ViewData; ViewData = viewData ?? new ViewData(context.ViewData); - Model = (TModel)ViewData.Model; + Model = ViewData.Model; InitHelpers(context); return base.RenderAsync(context, writer); diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualFileSystem.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualFileSystem.cs index 15de54b42e..511e2181e1 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualFileSystem.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualFileSystem.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using Microsoft.Owin.FileSystems; @@ -21,6 +20,7 @@ namespace Microsoft.AspNet.Mvc.Razor if (_fileSystem.TryGetFileInfo(translated, out fileInfo)) { fileInfo = new VirtualFile(subpath, fileInfo); + return true; } return false; } @@ -31,6 +31,7 @@ namespace Microsoft.AspNet.Mvc.Razor if (_fileSystem.TryGetDirectoryContents(translated, out contents)) { contents = contents.Select(c => new VirtualFile(subpath + '/' + c.Name, c)); + return true; } return false; } diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualPathViewFactory.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualPathViewFactory.cs index 0a423d0065..387dcb8cb4 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualPathViewFactory.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/VirtualPathViewFactory.cs @@ -1,16 +1,15 @@ using System; using System.Threading.Tasks; -using Microsoft.AspNet.Mvc.Razor; using Microsoft.Owin.FileSystems; namespace Microsoft.AspNet.Mvc.Razor { public class VirtualPathViewFactory : IVirtualPathViewFactory { - private readonly IFileSystem _fileSystem; + private readonly IVirtualFileSystem _fileSystem; private readonly IRazorCompilationService _compilationService; - public VirtualPathViewFactory(IFileSystem fileSystem, IRazorCompilationService compilationService) + public VirtualPathViewFactory(IVirtualFileSystem fileSystem, IRazorCompilationService compilationService) { _fileSystem = fileSystem; _compilationService = compilationService; diff --git a/src/Microsoft.AspNet.Mvc/View/ViewData.cs b/src/Microsoft.AspNet.Mvc/View/ViewData.cs index 9e36112701..7254075056 100644 --- a/src/Microsoft.AspNet.Mvc/View/ViewData.cs +++ b/src/Microsoft.AspNet.Mvc/View/ViewData.cs @@ -6,7 +6,8 @@ namespace Microsoft.AspNet.Mvc { public class ViewData : DynamicObject { - private Dictionary _data; + private readonly Dictionary _data; + private object _model; public ViewData() { @@ -15,7 +16,14 @@ namespace Microsoft.AspNet.Mvc public ViewData(ViewData source) { - _data = new Dictionary(source._data); + _data = source._data; + SetModel(source.Model); + } + + public object Model + { + get { return _model; } + set { SetModel(value); } } public dynamic this[string index] @@ -80,5 +88,13 @@ namespace Microsoft.AspNet.Mvc this[(string)index] = value; return true; } + + // This method will execute before the derived type's instance constructor executes. Derived types must + // be aware of this and should plan accordingly. For example, the logic in SetModel() should be simple + // enough so as not to depend on the "this" pointer referencing a fully constructed object. + protected virtual void SetModel(object value) + { + _model = value; + } } } diff --git a/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs b/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs index c4c3966eba..e2c99eddc0 100644 --- a/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs +++ b/src/Microsoft.AspNet.Mvc/View/ViewDataOfTModel.cs @@ -13,12 +13,16 @@ namespace Microsoft.AspNet.Mvc { } - public ViewData(ViewData source) - : base(source) + public new TModel Model { - Model = source.Model; + get { return (TModel)base.Model; } + set { SetModel(value); } } - public TModel Model { get; set; } + protected override void SetModel(object value) + { + // TODO: Add checks for cast + base.SetModel((TModel)value); + } } }