Update namespace generation in RazorCompilationService to use root

relative paths.
This commit is contained in:
Pranav K 2014-02-07 12:29:39 -08:00
parent 5e32b6380c
commit 3d6740bc8a
8 changed files with 26 additions and 18 deletions

View File

@ -1,6 +1,6 @@
using System.Text;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc;
using MvcSample.Models;
namespace MvcSample
{

View File

@ -1,5 +1,5 @@
using Microsoft.AspNet.Mvc;
using Microsoft.Owin;
using MvcSample.Models;
namespace MvcSample
{

View File

@ -1,4 +1,4 @@
namespace MvcSample
namespace MvcSample.Models
{
public class User
{

View File

@ -1,4 +1,4 @@

@using MvcSample.Models
@{
Layout = "/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Home Page";
@ -9,9 +9,11 @@
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Learn more &raquo;</a></p>
</div>
@{
var user = new User { Name = "Test user" };
}
<div class="row">
<h3>Hello!</h3>
<h3>Hello @user.Name!</h3>
<div class="col-md-4">
<h2>Getting started</h2>
<p>
@ -32,4 +34,3 @@
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
</div>
</div>

View File

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

View File

@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Mvc.Razor
{
public interface IRazorCompilationService
{
Task<CompilationResult> Compile(IFileInfo fileInfo);
Task<CompilationResult> Compile(string appRoot, IFileInfo fileInfo);
}
}

View File

@ -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<CompilationResult> Compile(IFileInfo file)
public Task<CompilationResult> Compile(string appRoot, IFileInfo file)
{
return _cache.GetOrAdd(file, () => CompileCore(file));
return _cache.GetOrAdd(file, () => CompileCore(appRoot, file));
}
private async Task<CompilationResult> CompileCore(IFileInfo file)
private async Task<CompilationResult> 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)

View File

@ -17,10 +17,12 @@ namespace Microsoft.AspNet.Mvc.Razor
public async Task<IView> 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);
}