Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hostname is always null in DynamicNodeProvider #365

Closed
Virtualhurst opened this issue Oct 17, 2014 · 2 comments
Closed

Hostname is always null in DynamicNodeProvider #365

Virtualhurst opened this issue Oct 17, 2014 · 2 comments

Comments

@Virtualhurst
Copy link

I have multiple sites running from a single code base but using mostly the same URLs. There is a difference in the products that are displayed for each site and I am able to determine the correct set of products for the DynamicNodeProvider to display based on the host name (if it is supplied).

However, the host name is always null using the attribute below:
[MvcSiteMapNodeAttribute(Title = "Product", ParentKey = "Home", DynamicNodeProvider = "Model.Sitemap.ProductsDynamicNodeProvider, Model", Protocol = "*")]

The documentation says that the current request's host name is used if the protocol value is set but this doesn't seem to be the case.

@NightOwl888
Copy link
Collaborator

The default setup will automatically create a separate SiteMap instance per domain name (or subdomain). What you need to do in this case is to load the SiteMapCacheKey into a System.Uri instance and then use the Host property when retrieving your products.

var siteMapCacheKey = node.SiteMap.CacheKey;
var cacheKeyUri = new Uri(siteMapCacheKey, UriKind.Absolute);
var hostName = cacheKeyUri.Host;

This example assumes you have not provided a custom ISiteMapCacheKeyGenerator that changes the format of the SiteMapCacheKey string from a valid URI to another format.

Do note that the above code will throw an exception if a developer tries to use a non-URI as a SiteMapCacheKey when specifying the SiteMap instance to use for a HTML helper.

@Html.MvcSiteMap("SomeNonUriString").SiteMapPath()

The host name will default to the current host name when resolving the URL, but this is based on the fact that the node configuration has a null or empty string for a host name.

@NightOwl888
Copy link
Collaborator

As a side note, you led me to a bug in the default SiteMapCacheKeyGenerator. It should be favoring the "HTTP_HOST" header when present over the DnsSafeHost of the current request to derive the host name. This will ensure it will work consistently with the URL resolving behavior. This allows web farm and cloud environments a way to provide the host name to the application, since they will sometimes not be available in the request.

I have a patch in the works to fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants