Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Commit

Permalink
added: parent controller reference syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtoroq committed Sep 6, 2014
1 parent 7bb047d commit 149f1e3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
Expand Up @@ -9,7 +9,7 @@ namespace Samples.Controllers.Admin.Tasks {
public class MaintenanceController : Controller {

public ActionResult Index() {
throw new NotImplementedException();
return View();
}
}
}
15 changes: 15 additions & 0 deletions samples/WebHost/Controllers/Admin/Tasks/TasksController.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Samples.Controllers.Admin.Tasks {

public class TasksController : Controller {

public ActionResult Index() {
return View();
}
}
}
3 changes: 3 additions & 0 deletions samples/WebHost/Samples.csproj
Expand Up @@ -104,6 +104,7 @@
<Compile Include="Controllers\Admin\AdminController.cs" />
<Compile Include="Controllers\Admin\CrudController.cs" />
<Compile Include="Controllers\Admin\RoleController.cs" />
<Compile Include="Controllers\Admin\Tasks\TasksController.cs" />
<Compile Include="Controllers\Admin\Tasks\MaintenanceController.cs" />
<Compile Include="Controllers\Admin\UserController.cs" />
<Compile Include="Controllers\Api\ApiController.cs" />
Expand Down Expand Up @@ -132,6 +133,8 @@
<Content Include="Views\Account\LogOn.aspx" />
<Content Include="Views\Account\LogOnStatus.ascx" />
<Content Include="Views\Admin\Index.aspx" />
<Content Include="Views\Admin\Tasks\Index.aspx" />
<Content Include="Views\Admin\Tasks\Maintenance\Index.aspx" />
<Content Include="Views\Admin\User\Edit.aspx" />
<Content Include="Views\Admin\User\Index.aspx" />
<Content Include="Views\Home\Index.aspx" />
Expand Down
5 changes: 5 additions & 0 deletions samples/WebHost/Views/Admin/Tasks/Index.aspx
@@ -0,0 +1,5 @@
<%@ Page Language="C#" %>

<asp:Content ContentPlaceHolderID="content" runat="server">

</asp:Content>
5 changes: 5 additions & 0 deletions samples/WebHost/Views/Admin/Tasks/Maintenance/Index.aspx
@@ -0,0 +1,5 @@
<%@ Page Language="C#" %>

<asp:Content ContentPlaceHolderID="content" runat="server">

</asp:Content>
14 changes: 11 additions & 3 deletions samples/WebHost/Views/Shared/UrlGenerationSamples.ascx
Expand Up @@ -42,12 +42,20 @@
</tr>
<tr>
<td>
<code>Url.Action("", "..Admin")</code>
<code>Url.Action("", "..")</code>
</td>
<td><%: Url.Action("", "~Admin") %></td>
<td><%: Url.Action("", "..Admin") %></td>
<td><i>(varies)</i></td>
<td><%: Url.Action("", "..") %></td>
<td>Parent reference.</td>
</tr>
<tr>
<td>
<code>Url.Action("", "..User")</code>
</td>
<td><i>(varies)</i></td>
<td><%: Url.Action("", "..User") %></td>
<td>Sibling of parent reference.</td>
</tr>
<tr>
<td>
<code>Url.Action("", "~Admin")</code>
Expand Down
52 changes: 42 additions & 10 deletions src/MvcCodeRouting/ICodeRoute.cs
Expand Up @@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcCodeRouting.Controllers;

namespace MvcCodeRouting {

Expand Down Expand Up @@ -107,22 +108,24 @@ static class CodeRouteExtensions {

static string GetRouteContext(IDictionary<string, object> values, IDictionary<string, object> requestRouteValues, IDictionary<string, object> requestRouteDataTokens, out string originalController) {

object origControllerObj;
originalController = null;

if (values.ContainsKey("controller")) {
originalController = (string)values["controller"];
if (values.TryGetValue("controller", out origControllerObj)) {
originalController = (string)origControllerObj;
}

const string routeContextKey = "__routecontext";
object routeContextParamObj;

if (values.ContainsKey(routeContextKey)) {
return (string)values[routeContextKey];
if (values.TryGetValue("__routecontext", out routeContextParamObj)) {
return (string)routeContextParamObj;
}


object routeContextObj;
string routeContext = null;

if (requestRouteDataTokens.ContainsKey(DataTokenKeys.RouteContext)) {
routeContext = (string)requestRouteDataTokens[DataTokenKeys.RouteContext];
if (requestRouteDataTokens.TryGetValue(DataTokenKeys.RouteContext, out routeContextObj)) {
routeContext = (string)routeContextObj;
}

if (routeContext == null) {
Expand Down Expand Up @@ -176,6 +179,34 @@ static class CodeRouteExtensions {

routeContextSegments.RemoveAt(routeContextSegments.Count - 1);
theController.Remove(0, 2);

if (theController.Length == 0) {

object namespacesObj;
string[] namespaces;

if (requestRouteDataTokens.TryGetValue(DataTokenKeys.Namespaces, out namespacesObj)
&& (namespaces = namespacesObj as string[]) != null
&& namespaces.Length == 1) {

string[] namespaceSegments = namespaces[0].Split('.');
string nsLast = namespaceSegments[namespaceSegments.Length - 1];
string currentController = (string)requestRouteValues["controller"];

string parentController = nsLast;

if (ControllerInfo.NameEquals(nsLast, currentController)
&& namespaceSegments.Length > 1) {

parentController = namespaceSegments
.Skip(namespaceSegments.Length - 2)
.Take(1)
.First();
}

theController.Append(parentController);
}
}
}

if (theController.Length > 1) {
Expand Down Expand Up @@ -208,15 +239,16 @@ static class CodeRouteExtensions {
}

originalValue = originalObject as string;
string mappedValue;

if (originalValue == null
|| mapping == null
|| !mapping.ContainsKey(originalValue)) {
|| !mapping.TryGetValue(originalValue, out mappedValue)) {

return false;
}

values[name] = mapping[originalValue];
values[name] = mappedValue;

return true;
}
Expand Down

0 comments on commit 149f1e3

Please sign in to comment.