Skip to content

Commit

Permalink
Added HermaFx.Mvc project.
Browse files Browse the repository at this point in the history
  • Loading branch information
pruiz committed Jul 7, 2016
1 parent a46ab16 commit 8959e14
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 12 deletions.
175 changes: 175 additions & 0 deletions HermaFx.Mvc/FeatureBasedRazorViewEngine.cs
@@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HermaFx.Mvc
{
public class FeatureBasedRazorViewEngine : RazorViewEngine
{
//holds all of the actual paths to the required files
private static ConcurrentDictionary<string, string> _ResolvedFilePaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public FeatureBasedRazorViewEngine()
{
// {0} ActionName; {1} ControllerName; {2} AreaName

FileExtensions = new[]
{
"cshtml"
};

ViewLocationFormats = new[]
{
"~/Features/{1}/{1}{0}.cshtml",
"~/Features/Shared/{0}.cshtml"
};

MasterLocationFormats = ViewLocationFormats;

PartialViewLocationFormats = new[]
{
"~/Features/{1}/{1}{0}.cshtml",
"~/Features/Shared/{0}.cshtml"
};

AreaViewLocationFormats = new[]
{
"~/Areas/{2}/{1}/{1}{0}.cshtml",
"~/Areas/{2}/Shared/{0}.cshtml" // Replacement for "Views/Shared"
};

AreaMasterLocationFormats = AreaViewLocationFormats;

AreaPartialViewLocationFormats = new[]
{
"~/Areas/{2}/{1}/_{1}{0}.cshtml",
"~/Areas/{2}/Shared/{0}.cshtml" // Replacement for "Views/Shared"
};
}

#region Get Root Directory
private string _AppRootPath = null;

private string GetRootDirectory()
{
lock (this)
{
if (_AppRootPath == null)
{
_AppRootPath = HttpContext.Current.Server.MapPath("~/");
}

return _AppRootPath;
}
}
#endregion

#region Internal Methods

// searches for a matching file name in the current directory
private string LookupFile(DirectoryInfo folder, string name)
{
//try and find a matching file, regardless of case
var match = folder.EnumerateFiles().FirstOrDefault(file => file.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
return match is FileInfo ? match.Name : null;
}

// searches for a folder in the current directory and steps up a level
private string LookupDirectory(ref DirectoryInfo folder, string name)
{
folder = folder.EnumerateDirectories().FirstOrDefault(dir => dir.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
return folder is DirectoryInfo ? folder.Name : null;
}

// determines (and caches) the actual path for a file
private string ResolveActualFilePath(string virtualPath)
{
// get the root folder to work from
var folder = new DirectoryInfo(GetRootDirectory());
var segments = virtualPath.Split(new char[] { '/' });

// start stepping up the folders to replace with the correct cased folder name
for (int i = 0; i < segments.Length; i++)
{
var part = segments[i];
var last = i == segments.Length - 1;

// ignore the root
if (part.Equals("~")) continue;

// process the file name if this is the last segment
else if (last) part = LookupFile(folder, part);

// step up the directory for another part
else part = this.LookupDirectory(ref folder, part);

// if no matches were found, just return null
if (part == null || folder == null) return null;

// update the segment with the correct name
segments[i] = part;
}

return string.Join("/", segments);
}

private string GetActualFilePath(string virtualPath)
{
// check if this has already been matched before
if (_ResolvedFilePaths.ContainsKey(virtualPath))
return _ResolvedFilePaths[virtualPath];

var result = ResolveActualFilePath(virtualPath);
if (result != null)
{
_ResolvedFilePaths.TryAdd(virtualPath, result);
}

return result ?? virtualPath;
}

#endregion

#region IViewEngine Overrides

/// <summary>
/// Creates the view.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="viewPath">The view path.</param>
/// <param name="masterPath">The master path.</param>
/// <returns></returns>
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return base.CreateView(controllerContext, GetActualFilePath(viewPath), GetActualFilePath(masterPath));
}

/// <summary>
/// Creates the partial view.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="partialPath">The partial path.</param>
/// <returns></returns>
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return base.CreatePartialView(controllerContext, GetActualFilePath(partialPath));
}

/// <summary>
/// Files the exists.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="virtualPath">The virtual path.</param>
/// <returns></returns>
protected override bool FileExists(ControllerContext context, string virtualPath)
{
return base.FileExists(context, GetActualFilePath(virtualPath));
}

#endregion
}
}
92 changes: 92 additions & 0 deletions HermaFx.Mvc/HermaFx.Mvc.csproj
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{36FB4448-8B60-4A08-A79F-F20FFCCE6A8B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HermaFx.Mvc</RootNamespace>
<AssemblyName>HermaFx.Mvc</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.WebPages.Deployment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FeatureBasedRazorViewEngine.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="HermaFx.Mvc.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HermaFx.Foundation\HermaFx.Foundation.csproj">
<Project>{0c13f477-7360-4831-9d62-9c5b183224c8}</Project>
<Name>HermaFx.Foundation</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
23 changes: 23 additions & 0 deletions HermaFx.Mvc/HermaFx.Mvc.nuspec
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>HermaFx.Mvc</id>
<version>0.0.0.0</version>
<authors>Evidencias Certificadas, S.L.</authors>
<owners>Evidencias Certificadas, S.L.</owners>
<projectUrl>https://github.com/evicertia/HermaFx</projectUrl>
<licenseUrl>http://www.evicertia.com/</licenseUrl>
<!-- iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl -->
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>See changes for this revision at: https://github.com/evicertia/HermaFx/commit/GIT_COMMIT </releaseNotes>
<description>HermaFx Mvc extensions.</description>
<copyright>Copyright Evidencias Certificadas, S.L. 2010-2016</copyright>
<dependencies>
<dependency id="HermaFx.Foundation" version="0.0.0.0" />
</dependencies>
</metadata>
<files>
<file src="bin\Release\Herma.Mvc.*" target="lib\NET45" />
<file src="**\*.cs" target="src" />
</files>
</package>
36 changes: 36 additions & 0 deletions HermaFx.Mvc/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HermaFx.Mvc")]
[assembly: AssemblyDescription("HermaFx Microsoft.Web.Mvc Extensions")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Evidencias Certificadas S.L.")]
[assembly: AssemblyProduct("HermaFx.Mvc")]
[assembly: AssemblyCopyright("Copyright © Evidencias Certificadas S.L. 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("36fb4448-8b60-4a08-a79f-f20ffcce6a8b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: AssemblyFileVersion("0.0.0.0")]
7 changes: 7 additions & 0 deletions HermaFx.Mvc/packages.config
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
</packages>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions HermaFx.MvcDemo/Global.asax.cs 100755 → 100644
Expand Up @@ -4,6 +4,8 @@
using System.Web.Mvc;
using System.Web.Routing;

using HermaFx.Mvc;

namespace HermaFx.MvcDemo
{
public class Global : System.Web.HttpApplication
Expand All @@ -13,6 +15,8 @@ protected void Application_Start(object sender, EventArgs e)
{
FilterConfig.Configure(GlobalFilters.Filters);
RouteConfig.Configure(RouteTable.Routes);
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new FeatureBasedRazorViewEngine());
}

protected void Session_Start(object sender, EventArgs e)
Expand Down

0 comments on commit 8959e14

Please sign in to comment.