Welcome to Vortx Community Forum, where you can ask questions and receive answers from the staff at Vortx and other members of the community.

If you had a user account on our previous forums website, you will need to register a new account here.

Learn more about...

AspDotNetStorefront
DotFeed

.GetThisPageName(true) still returns .../p-1234.aspx - any alternative suggestions?

http://@Request.Url.Host@CommonLogic.GetThisPageName(true) still still returns ".../p-1234.aspx" - is there an alternative to try?

asked May 4, 2018 in MultiStore by Andre (125 points)

1 Answer

0 votes
GetThisPageName uses the current page (the SCRIPT_NAME ServerVariable to be excat) to generate the url. In the linked post it the urls still used .aspx pages so that wouldn't be an issue. If you are using the Morden MVC routes most .aspx pages should be redirecting to begin with also. So not entirely sure I follow the full context.

But I guess you could try to use Url.BuildProductLink instead. Which would look something like http://@Request.Url.Host@Url.BuildProductLink(Convert.ToInt32(AppLogic.GetCurrentPageID()), null ,null) . Do keep in mind I haven't tested this fully and you may need a if statement check or tryparse for AppLogic.GetCurrentPageID being emtpy.

Also if you are getting this complex with meta tags I suggest adding a custom class. Since a class can be easier to deal with than having all code in a MVC view. That way you can hadle each page type.
answered May 4, 2018 by mmcgeachy (5,045 points)
Thank you.

This is still returning (for example) ".../c-33.aspx" instead of ".../c-33-boots.aspx" - the SEName is stil being omitted, but I understand your point about a custom class where we can deal with this.

Does your URL have a SEname in it? If it is blank than the defualt code should do a 301 to URL to add the SEname. Which I thought CommonLogic.GetThisPageName(true) pulled through the SEname when present.

If the SEname is in the URL and just not showing in the in CommonLogic.GetThisPageName(true) the code below may work.

@{
	var sename = string.Empty;
	var routeData = HttpContext.Current.Request.RequestContext.RouteData.Values;
	if (routeData != null || routeData.ContainsKey(RouteDataKeys.SearchEngineName))
	{
		if(!string.IsNullOrWhiteSpace(routeData[RouteDataKeys.SearchEngineName] as string))
		{
			sename = (routeData[RouteDataKeys.SearchEngineName] as string);
		}
	}	

}
http://@Request.Url.Host@Url.BuildProductLink(Convert.ToInt32(AppLogic.GetCurrentPageID()), sename, null)

Which is basicly just passing through the SEname from routedata to Url.BuildProductLink

Thank you. 

routeData.ContainsKey(RouteDataKeys.SearchEngineName) was returning as false, but I realised where I was going wrong - I only now realised the BuildProductLink is only for product pages - duh!:

On product pages the following returns the URL with SEName in the required format:

<link rel="canonical" href="https://@Request.Url.Host@Url.BuildProductLink(Convert.ToInt32(AppLogic.GetCurrentPageID()), null ,null)" />

On category pages the following returns the full URL with SEName in the correct format::

<link rel="canonical" href="https://@Request.Url.Host@Url.BuildEntityLink("category",Convert.ToInt32(AppLogic.GetCurrentPageID()), null ,null)" />

Therefore, the (rough) solution was as follows:
@{
switch (AppLogic.GetCurrentPageType())
{
case "category":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("category", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "product":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildProductLink(Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
}
}
 
Thank you for your help.
The above solution works for products and categpories, but what about manufacturer's and sections?

This does the other types of product listing type pages, but it should have code for the home page and topics pages also but which I do not know how to do.

@{
switch (AppLogic.GetCurrentPageType())
{
case "category":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("category", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "section":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("section", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "manufacturer":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("manufacturer", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "product":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildProductLink(Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
}
}

@kirk48906

Does you exmaple code have isssues with sections and manufacturers showing correctly? Cause it make sense at a glance.

For the topic it mostly comes down to using Url.BuildTopicLink. for the homepage it depends on if us you are using a virtual directory. If so using the GetEnvironment store class is needed.

Which makes the cshtml code roughly

@{
switch (AppLogic.GetCurrentPageType())
{
case "category":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("category", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "section":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("section", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "manufacturer":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildEntityLink("manufacturer", Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "product":
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + Url.BuildProductLink(Convert.ToInt32(AppLogic.GetCurrentPageID()), null, null) + "\" />");
break;
case "topic":
string topicname = CommonLogic.QueryStringCanBeDangerousContent("name");
@Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host +  Url.BuildTopicLink(topicname) + "\" />");
break;
case "home":
int StoreID = AspDotNetStorefront.Core.AppLogic.StoreID();
var currentEnvironment = AspDotNetStorefront.Core.Store.GetEnvironment(currentDomain: System.Web.HttpContext.Current.Request.Url.Host);
var storeDomain = AspDotNetStorefront.Core.Store.GetStoreDomainByEnvironment(currentEnvironment, StoreID);
var virtualDirectory = AspDotNetStorefront.Core.Store.GetStoreDirectoryByEnvironment(currentEnvironment, StoreID);
@Html.Raw("<link rel=\"canonical\" href=\"https://" + storeDomain + virtualDirectory + "\" />");
break;
}
}

Please keep in mind the code above is a mockup of the code. Since I use a class to do this instead. Also the virtual directory check might be skipable by most for homepage. Making it so you can get away with @Html.Raw("<link rel=\"canonical\" href=\"https://" + Request.Url.Host + "\" />"); instead.

Hope that example helps

That works perfectly.  I had disceoivered the section and manufacturer code was the same by playing (I'm not a net programer, but dangerous enough to break things).

This code should have been included in the code a long time ago.  Google goes from not using certain fields as much, to making them much more dependent on them for search results.
...