Skip to content

Commit

Permalink
Merge pull request #69 from davidsk/EntryPointResolverUpdates
Browse files Browse the repository at this point in the history
Added checks for baseUrl format
  • Loading branch information
stefanprodan committed Jul 15, 2016
2 parents db11f41 + 0f6fc71 commit 5b14953
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require([], function() {
alert("hello world");
alert("home/entrypoint");
});
3 changes: 2 additions & 1 deletion RequireJsNet.Examples/App_Start/RouteConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static void RegisterRoutes(RouteCollection routes)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "RequireJsNet.Examples.Controllers" }
);
}
}
Expand Down
17 changes: 17 additions & 0 deletions RequireJsNet.Examples/Areas/Foo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RequireJsNet.Examples.Controllers;

namespace RequireJsNet.Examples.Areas.Foo.Controllers
{
public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}
}
}
25 changes: 25 additions & 0 deletions RequireJsNet.Examples/Areas/Foo/FooAreaRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Web.Mvc;

namespace RequireJsNet.Examples.Areas.Foo
{
public class FooAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Foo";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Foo_default",
"Foo/{controller}/{action}/{id}",
new { controller="Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "RequireJsNet.Examples.Areas.Foo.Controllers" }
);
}
}
}
32 changes: 32 additions & 0 deletions RequireJsNet.Examples/Areas/Foo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@{
ViewBag.Title = "Home Page";
Layout = "~\\Views\\Shared\\_LayoutSimple.cshtml";
}

<div class="jumbotron">
<h1>ASP.NET</h1>
<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-lg">Learn more &raquo;</a></p>
</div>

<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and gives you full control over markup
for enjoyable, agile development.
</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
</div>
</div>
36 changes: 36 additions & 0 deletions RequireJsNet.Examples/Areas/Foo/Views/web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>

<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
<add namespace="RequireJsNet.Examples" />

</namespaces>
</pages>
</system.web.webPages.razor>

<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>

<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
2 changes: 1 addition & 1 deletion RequireJsNet.Examples/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ActionResult Complex()
return View();
}

public ActionResult FooBar()
public ActionResult EntryPoint()
{
return View();
}
Expand Down
11 changes: 9 additions & 2 deletions RequireJsNet.Examples/RequireJsNet.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Areas\Foo\FooAreaRegistration.cs" />
<Compile Include="Areas\Foo\Controllers\HomeController.cs" />
<Compile Include="Attributes\RequireOptionFilter.cs" />
<Compile Include="Controllers\BaseController.cs" />
<Compile Include="Controllers\HomeController.cs" />
Expand All @@ -122,7 +124,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App\Controllers\Root\Home\FooBar.js" />
<Content Include="App\Controllers\Root\Home\EntryPoint.js" />
<Content Include="Content\bootstrap-theme.css" />
<Content Include="Content\bootstrap-theme.min.css" />
<Content Include="Content\bootstrap.css" />
Expand All @@ -140,6 +142,8 @@
<Content Include="Content\bootstrap-theme.css.map" />
<Content Include="Content\bootstrap.css.map" />
<Content Include="RequireJS.entrypoint.json" />
<Content Include="Areas\Foo\Views\web.config" />
<Content Include="Areas\Foo\Views\Home\Index.cshtml" />
<None Include="Scripts\amplify-vsdoc.js" />
<Content Include="Scripts\amplify.js" />
<Content Include="Scripts\amplify.min.js" />
Expand All @@ -153,6 +157,7 @@
<Content Include="RequireJS.complex.json" />
<Content Include="Scripts\bootstrap.js" />
<Content Include="Scripts\bootstrap.min.js" />
<Content Include="Scripts\Controllers\Foo\Home\index.js" />
<Content Include="Scripts\Controllers\Root\Home\complexLoad.js" />
<Content Include="Scripts\domReady.js" />
<None Include="Scripts\jquery-2.1.3.intellisense.js" />
Expand Down Expand Up @@ -198,11 +203,13 @@
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Shared\_LayoutSimple.cshtml" />
<Content Include="Views\Home\Complex.cshtml" />
<Content Include="Views\Home\FooBar.cshtml" />
<Content Include="Views\Home\EntryPoint.cshtml" />
<Content Include="Views\Shared\_LayoutEntrypoint.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Areas\Foo\Models\" />
<Folder Include="Areas\Foo\Views\Shared\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions RequireJsNet.Examples/Scripts/Controllers/Foo/Home/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(['jquery', 'domReady'], function() {
alert("foo/home/index");
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require([], function() {

alert("home/basic");
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require(['jquery', 'domReady'], function() {
console.log("home/index loaded");
alert("home/index");
});
62 changes: 25 additions & 37 deletions RequireJsNet/EntryPointResolver/DefaultEntryPointResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,65 +23,53 @@ public virtual string Resolve(ViewContext viewContext, string baseUrl, string en
entryPointRoot = baseUrl;
}

var resolvedBaseUrl = UrlHelper.GenerateContentUrl(baseUrl, viewContext.HttpContext);
var resolvedEntryPointRoot = UrlHelper.GenerateContentUrl(entryPointRoot, viewContext.HttpContext);

if (resolvedEntryPointRoot != baseUrl)

if (resolvedEntryPointRoot != resolvedBaseUrl)
{
// entryPointRoot is different from default.
if ((entryPointRoot.StartsWith("~") || entryPointRoot.StartsWith("/")))
{
// entryPointRoot is defined as root relative, do not use with baseUrl
withBaseUrl = false;
rootUrl = UrlHelper.GenerateContentUrl(entryPointRoot, viewContext.HttpContext);
rootUrl = resolvedEntryPointRoot;
}
else
{
// entryPointRoot is defined relative to baseUrl; prepend baseUrl
entryPointRoot = baseUrl + entryPointRoot;
resolvedEntryPointRoot = resolvedBaseUrl + entryPointRoot;
}
}

// search for controller/action.js in current area
var entryPointTmpl = "Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Action;
var entryPoint = string.Format(entryPointTmpl, routingInfo.Area).ToModuleName();
var filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
var entryPointTemplates = new[]
{
var computedEntry = GetEntryPoint(server, filePath, baseUrl);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}
"Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Action,
"Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Controller + "-" + routingInfo.Action
};

// search for controller/action.js in common area
entryPoint = string.Format(entryPointTmpl, DefaultArea).ToModuleName();
filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
var areas = new[]
{
var computedEntry = GetEntryPoint(server, filePath, baseUrl);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}

// search for controller/controller-action.js in current area
entryPointTmpl = "Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Controller + "-" + routingInfo.Action;
entryPoint = string.Format(entryPointTmpl, routingInfo.Area).ToModuleName();
filePath = server.MapPath(entryPointRoot + entryPoint + ".js");
routingInfo.Area,
DefaultArea
};

if (File.Exists(filePath))
foreach (var entryPointTmpl in entryPointTemplates)
{
var computedEntry = GetEntryPoint(server, filePath, baseUrl);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
foreach (var area in areas)
{
var entryPoint = string.Format(entryPointTmpl, area).ToModuleName();
var filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, baseUrl);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}
}
}

// search for controller/controller-action.js in common area
entryPoint = string.Format(entryPointTmpl, DefaultArea).ToModuleName();
filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, baseUrl);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}

return null;
}
Expand Down

0 comments on commit 5b14953

Please sign in to comment.