diff --git a/samples/MvcSample/Home2Controller.cs b/samples/MvcSample/Home2Controller.cs index cd2abb49ad..59d84891aa 100644 --- a/samples/MvcSample/Home2Controller.cs +++ b/samples/MvcSample/Home2Controller.cs @@ -1,6 +1,6 @@ -using System.Text; -using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Mvc; +using MvcSample.Models; namespace MvcSample { diff --git a/samples/MvcSample/HomeController.cs b/samples/MvcSample/HomeController.cs index 3e298ca8df..db8481ef11 100644 --- a/samples/MvcSample/HomeController.cs +++ b/samples/MvcSample/HomeController.cs @@ -1,5 +1,5 @@ using Microsoft.AspNet.Mvc; -using Microsoft.Owin; +using MvcSample.Models; namespace MvcSample { diff --git a/samples/MvcSample/Models/Class1.cs b/samples/MvcSample/Models/User.cs similarity index 79% rename from samples/MvcSample/Models/Class1.cs rename to samples/MvcSample/Models/User.cs index cc453843e6..877d54e0b0 100644 --- a/samples/MvcSample/Models/Class1.cs +++ b/samples/MvcSample/Models/User.cs @@ -1,4 +1,4 @@ -namespace MvcSample +namespace MvcSample.Models { public class User { diff --git a/samples/MvcSample/Views/Home/MyView.cshtml b/samples/MvcSample/Views/Home/MyView.cshtml index c84e688d5b..8e873628c0 100644 --- a/samples/MvcSample/Views/Home/MyView.cshtml +++ b/samples/MvcSample/Views/Home/MyView.cshtml @@ -1,4 +1,4 @@ - +@using MvcSample.Models @{ Layout = "/Views/Shared/_Layout.cshtml"; ViewBag.Title = "Home Page"; @@ -9,9 +9,11 @@

ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.

Learn more »

- +@{ + var user = new User { Name = "Test user" }; +}
-

Hello!

+

Hello @user.Name!

Getting started

@@ -32,4 +34,3 @@

Learn more »

- diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs index c15970ba1e..be3cda37cb 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/CscBasedCompilationService.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using Microsoft.AspNet.FileSystems; namespace Microsoft.AspNet.Mvc.Razor { @@ -118,8 +117,6 @@ namespace Microsoft.AspNet.Mvc.Razor return process; } - - } } #endif \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Razor/IRazorCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Razor/IRazorCompilationService.cs index b22c9af035..0db3d7cebc 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Razor/IRazorCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Razor/IRazorCompilationService.cs @@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Mvc.Razor { public interface IRazorCompilationService { - Task Compile(IFileInfo fileInfo); + Task Compile(string appRoot, IFileInfo fileInfo); } } diff --git a/src/Microsoft.AspNet.Mvc.Razor/Razor/RazorCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Razor/RazorCompilationService.cs index 3c9efdc91e..68d90dfdd3 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Razor/RazorCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Razor/RazorCompilationService.cs @@ -1,7 +1,7 @@ using System; +using System.Diagnostics.Contracts; using System.IO; using System.Linq; -using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.FileSystems; using Microsoft.AspNet.Razor; @@ -20,17 +20,25 @@ namespace Microsoft.AspNet.Mvc.Razor _razorHost = razorHost; } - public Task Compile(IFileInfo file) + public Task Compile(string appRoot, IFileInfo file) { - return _cache.GetOrAdd(file, () => CompileCore(file)); + return _cache.GetOrAdd(file, () => CompileCore(appRoot, file)); } - private async Task CompileCore(IFileInfo file) + private async Task CompileCore(string appRoot, IFileInfo file) { GeneratorResults results; using (Stream inputStream = file.CreateReadStream()) { - results = _razorHost.GenerateCode(file.PhysicalPath, inputStream); + Contract.Assert(file.PhysicalPath.StartsWith(appRoot, StringComparison.OrdinalIgnoreCase)); + // Remove the app name segment so that it appears as part of the root relative path: + // work/src/myapp/ -> work/src + // root relative path: myapp/views/home/index.cshtml + // TODO: The root namespace might be a property we'd have to read via configuration since it + // affects other things such as resx files. + appRoot = Path.GetDirectoryName(appRoot.TrimEnd(Path.DirectorySeparatorChar)); + string rootRelativePath = file.PhysicalPath.Substring(appRoot.Length).TrimStart(Path.DirectorySeparatorChar); + results = _razorHost.GenerateCode(rootRelativePath, inputStream); } if (!results.Success) diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/PathBasedViewFactory.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/PathBasedViewFactory.cs index 2225b8d58d..69c90fd628 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/PathBasedViewFactory.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/PathBasedViewFactory.cs @@ -17,10 +17,12 @@ namespace Microsoft.AspNet.Mvc.Razor public async Task CreateInstance(string virtualPath) { + // TODO: We need to glean the approot from HttpContext + var appRoot = ((PhysicalFileSystem)_fileSystem).Root; IFileInfo fileInfo; if (_fileSystem.TryGetFileInfo(virtualPath, out fileInfo)) { - CompilationResult result = await _compilationService.Compile(fileInfo); + CompilationResult result = await _compilationService.Compile(appRoot, fileInfo); return (IView)Activator.CreateInstance(result.CompiledType); }