Skip to content

Commit

Permalink
Fixes #230, change default HTTP method to GET since our HTML helpers …
Browse files Browse the repository at this point in the history
…do GET by default anyway. Also added HttpMethod property to DynamicNode and MvcSiteMapNodeAttribute so HttpMethod can be set.
  • Loading branch information
NightOwl888 committed Sep 21, 2013
1 parent d5e6a4e commit ee4f6f8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa
// Determine controller and (index) action
string controller = type.Name.Substring(0, type.Name.IndexOf("Controller"));
string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index";
string httpMethod = "*";
if (methodInfo != null)
{
// handle ActionNameAttribute
Expand All @@ -416,15 +415,10 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa
{
action = actionNameAttribute.Name;
}

// handle AcceptVerbsAttribute
var acceptVerbsAttribute = methodInfo.GetCustomAttributes(typeof(AcceptVerbsAttribute), true).FirstOrDefault() as AcceptVerbsAttribute;
if (acceptVerbsAttribute != null)
{
httpMethod = string.Join(",", acceptVerbsAttribute.Verbs.ToArray());
}
}

string httpMethod = String.IsNullOrEmpty(attribute.HttpMethod) ? HttpVerbs.Get.ToString().ToUpperInvariant() : attribute.HttpMethod.ToUpperInvariant();

// Handle title and description
var title = attribute.Title;
var description = String.IsNullOrEmpty(attribute.Description) ? title : attribute.Description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromMvcSiteMapNodeA
// Determine controller and (index) action
string controller = type.Name.Substring(0, type.Name.IndexOf("Controller"));
string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index";
string httpMethod = "*";

if (methodInfo != null)
{
// handle ActionNameAttribute
Expand All @@ -209,15 +209,10 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromMvcSiteMapNodeA
{
action = actionNameAttribute.Name;
}

// handle AcceptVerbsAttribute
var acceptVerbsAttribute = methodInfo.GetCustomAttributes(typeof(AcceptVerbsAttribute), true).FirstOrDefault() as AcceptVerbsAttribute;
if (acceptVerbsAttribute != null)
{
httpMethod = string.Join(",", acceptVerbsAttribute.Verbs.ToArray());
}
}

string httpMethod = String.IsNullOrEmpty(attribute.HttpMethod) ? HttpVerbs.Get.ToString().ToUpperInvariant() : attribute.HttpMethod.ToUpperInvariant();

// Handle title and description
var title = attribute.Title;
var description = String.IsNullOrEmpty(attribute.Description) ? title : attribute.Description;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using MvcSiteMapProvider.Xml;
using MvcSiteMapProvider.Collections.Specialized;
Expand Down Expand Up @@ -114,17 +115,21 @@ protected virtual ISiteMapNode GetSiteMapNodeFromXmlElement(ISiteMap siteMap, XE
//// Get area, controller and action from node declaration
string area = node.GetAttributeValue("area");
string controller = node.GetAttributeValue("controller");
string httpMethod = node.GetAttributeValueOrFallback("httpMethod", HttpVerbs.Get.ToString()).ToUpperInvariant();
// Handle title and description
var title = node.GetAttributeValue("title");
var description = String.IsNullOrEmpty(node.GetAttributeValue("description")) ? title : node.GetAttributeValue("description");

// Generate key for node
string key = nodeKeyGenerator.GenerateKey(
parentNode == null ? "" : parentNode.Key,
node.GetAttributeValue("key"),
node.GetAttributeValue("url"),
node.GetAttributeValue("title"),
title,
area,
controller,
node.GetAttributeValue("action"),
node.GetAttributeValueOrFallback("httpMethod", "*").ToUpperInvariant(),
httpMethod,
!(node.GetAttributeValue("clickable") == "false"));

// Handle implicit resources
Expand All @@ -133,9 +138,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromXmlElement(ISiteMap siteMap, XE
// Create node
ISiteMapNode siteMapNode = siteMapNodeFactory.Create(siteMap, key, implicitResourceKey);

// Handle title and description
var title = node.GetAttributeValue("title");
var description = String.IsNullOrEmpty(node.GetAttributeValue("description")) ? title : node.GetAttributeValue("description");


// Assign defaults
siteMapNode.Title = title;
Expand All @@ -147,7 +150,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromXmlElement(ISiteMap siteMap, XE
siteMapNode.DynamicNodeProvider = node.GetAttributeValue("dynamicNodeProvider");
siteMapNode.ImageUrl = node.GetAttributeValue("imageUrl");
siteMapNode.TargetFrame = node.GetAttributeValue("targetFrame");
siteMapNode.HttpMethod = node.GetAttributeValueOrFallback("httpMethod", "*").ToUpperInvariant();
siteMapNode.HttpMethod = httpMethod;
siteMapNode.Url = node.GetAttributeValue("url");
siteMapNode.CacheResolvedUrl = bool.Parse(node.GetAttributeValueOrFallback("cacheResolvedUrl", "true"));
siteMapNode.CanonicalUrl = node.GetAttributeValue("canonicalUrl");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using MvcSiteMapProvider.Xml;
using MvcSiteMapProvider.Collections.Specialized;
Expand Down Expand Up @@ -188,17 +189,21 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromXmlElement(XEle
string area = node.GetAttributeValue("area");
string controller = node.GetAttributeValue("controller");
var parentKey = parentNode == null ? "" : parentNode.Key;
var httpMethod = node.GetAttributeValueOrFallback("httpMethod", HttpVerbs.Get.ToString()).ToUpperInvariant();
// Handle title and description
var title = node.GetAttributeValue("title");
var description = String.IsNullOrEmpty(node.GetAttributeValue("description")) ? title : node.GetAttributeValue("description");

// Generate key for node
string key = helper.CreateNodeKey(
parentKey,
node.GetAttributeValue("key"),
node.GetAttributeValue("url"),
node.GetAttributeValue("title"),
title,
area,
controller,
node.GetAttributeValue("action"),
node.GetAttributeValueOrFallback("httpMethod", "*").ToUpperInvariant(),
httpMethod,
!(node.GetAttributeValue("clickable") == "false"));

// Handle implicit resources
Expand All @@ -208,10 +213,6 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromXmlElement(XEle
var nodeParentMap = helper.CreateNode(key, parentKey, SourceName, implicitResourceKey);
var siteMapNode = nodeParentMap.Node;

// Handle title and description
var title = node.GetAttributeValue("title");
var description = String.IsNullOrEmpty(node.GetAttributeValue("description")) ? title : node.GetAttributeValue("description");

// Assign defaults
siteMapNode.Title = title;
siteMapNode.Description = description;
Expand All @@ -222,7 +223,7 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromXmlElement(XEle
siteMapNode.DynamicNodeProvider = node.GetAttributeValue("dynamicNodeProvider");
siteMapNode.ImageUrl = node.GetAttributeValue("imageUrl");
siteMapNode.TargetFrame = node.GetAttributeValue("targetFrame");
siteMapNode.HttpMethod = node.GetAttributeValueOrFallback("httpMethod", "*").ToUpperInvariant();
siteMapNode.HttpMethod = httpMethod;
siteMapNode.Url = node.GetAttributeValue("url");
siteMapNode.CacheResolvedUrl = bool.Parse(node.GetAttributeValueOrFallback("cacheResolvedUrl", "true"));
siteMapNode.CanonicalUrl = node.GetAttributeValue("canonicalUrl");
Expand Down
10 changes: 10 additions & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/DynamicNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ public UpdatePriority UpdatePriority
/// <value>The sort order.</value>
public int? Order { get; set; }

/// <summary>
/// Gets or sets the HTTP method (such as GET, POST, or HEAD).
/// </summary>
/// <value>
/// The HTTP method.
/// </value>
public string HttpMethod { get; set; }

/// <summary>
/// Copies the values for matching properties on an <see cref="T:MvcSiteMapNodeProvider.ISiteMapNode"/> instance, but
/// doesn't overwrite any values that are not set in this <see cref="T:MvcSiteMapNodeProvider.DynamicNode"/> instance.
Expand Down Expand Up @@ -236,6 +244,8 @@ public void SafeCopyTo(ISiteMapNode node)
}
if (this.Order != null)
node.Order = (int)this.Order;
if (!string.IsNullOrEmpty(this.HttpMethod))
node.HttpMethod = this.HttpMethod;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,14 @@ public interface IMvcSiteMapNodeAttribute
/// </summary>
/// <value>The SiteMap cache key.</value>
string SiteMapCacheKey { get; set; }

/// <summary>
/// Gets or sets the HTTP method (such as GET, POST, or HEAD) to use to determine
/// node accessibility.
/// </summary>
/// <value>
/// The HTTP method.
/// </value>
string HttpMethod { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,14 @@ public MvcSiteMapNodeAttribute()
/// Gets or sets the name of the cache key this node is associated with
/// </summary>
public string SiteMapCacheKey { get; set; }

/// <summary>
/// Gets or sets the HTTP method (such as GET, POST, or HEAD) to use to determine
/// node accessibility.
/// </summary>
/// <value>
/// The HTTP method.
/// </value>
public string HttpMethod { get; set; }
}
}
11 changes: 9 additions & 2 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcSiteMapProvider.Globalization;
using MvcSiteMapProvider.Web;
Expand Down Expand Up @@ -75,6 +76,7 @@ IUrlPath urlPath
protected readonly ISiteMap siteMap;
protected readonly string key;
protected readonly bool isDynamic;
protected string httpMethod = HttpVerbs.Get.ToString().ToUpperInvariant();
protected string title = String.Empty;
protected string description = String.Empty;
protected string imageUrl = String.Empty;
Expand Down Expand Up @@ -114,12 +116,17 @@ public override ISiteMap SiteMap
}

/// <summary>
/// Gets or sets the HTTP method.
/// Gets or sets the HTTP method (such as GET, POST, or HEAD) to use to determine
/// node accessibility.
/// </summary>
/// <value>
/// The HTTP method.
/// </value>
public override string HttpMethod { get; set; }
public override string HttpMethod
{
get { return this.httpMethod; }
set { this.httpMethod = value; }
}

/// <summary>
/// Gets the implicit resource key (optional).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ public virtual RequestContext CreateRequestContext(ISiteMapNode node, RouteData
{
var httpContext = this.CreateHttpContext(node);
return new RequestContext(httpContext, routeData);

//if (httpContext.Handler is MvcHandler)
// return ((MvcHandler)httpContext.Handler).RequestContext;
//else if (httpContext.Handler is Page) // Fixes #15 for interop with ASP.NET Webforms
// return new RequestContext(httpContext, ((Page)HttpContext.Current.Handler).RouteData);
//else
// return new RequestContext(httpContext, routeData);
}

public virtual RequestContext CreateRequestContext()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Web;
using System.Web.Mvc;

namespace MvcSiteMapProvider.Web.Mvc
{
Expand Down Expand Up @@ -62,13 +63,16 @@ public override string HttpMethod
{
get
{
const StringComparison comparison = StringComparison.OrdinalIgnoreCase;
bool replaceMethod = this.node != null &&
!String.IsNullOrEmpty(this.node.HttpMethod) &&
!String.Equals(this.node.HttpMethod, "*", comparison);
return replaceMethod
? this.node.HttpMethod.Split(',')[0]
: base.HttpMethod;
bool useRequest = this.node == null ||
String.Equals(this.node.HttpMethod, "*") ||
String.Equals(this.node.HttpMethod, "request", StringComparison.InvariantCultureIgnoreCase);
if (!useRequest)
{
return String.IsNullOrEmpty(this.node.HttpMethod)
? HttpVerbs.Get.ToString().ToUpperInvariant()
: this.node.HttpMethod.ToUpperInvariant();
}
return base.HttpMethod;
}
}
}
Expand Down

0 comments on commit ee4f6f8

Please sign in to comment.