rehansaeed.github.io
rehansaeed.github.io copied to clipboard
[Comment] Canonical URL's for ASP.NET MVC
https://rehansaeed.com/canonical-urls-for-asp-net-mvc/
Jasvinder commented on 2016-01-06 12:00:15
URL Canonicalization seems to be quite new topic to me, being a newbie. Although, I have read some guides, but still i have one confusion, Do I need to add URL Canonical Tag in every page of my site?
Muhammad Rehan Saeed commented on 2016-01-07 16:27:18
URL Canonicalization seems to be quite new topic to me, being a newbie. Although, I have read some guides, but still i have one confusion, Do I need to add URL Canonical Tag in every page of my site?
It's just a filter, so you can if you want to. The main idea of it though is to put it at the root and then use the NoTrailingSlashAttribute or NoLowercaseQueryStringAttribute to tweak how it works when you have an exception to the rule.
Tiger commented on 2016-05-15 21:44:43
Fantastic post - great that you shown options in mvc5 and mvc6
Doron Grinzaig commented on 2016-10-08 21:11:29
Hi Rehan, I follow many of your post and work, great job man! thanks for helping the community!
Curious to know if you have something to automate things like these:
[Route("TermsOfUse", Name= "TermsOfUse")]
[Route("Home/TermsOfUse", Name = "HomeTermsOfUse")]
public ActionResult TermsOfUse()
{
if (Request.GetDisplayUrl().Contains("/Home"))
return RedirectToRoutePermanent("TermsOfUse");
return View();
}
[Route("Privacy", Name= "Privacy")]
[Route("Home/Privacy", Name= "HomePrivacy")]
public ActionResult Privacy()
{
if (Request.GetDisplayUrl().Contains("/Home"))
return RedirectToRoutePermanent("Privacy");
return View();
}
Muhammad Rehan Saeed commented on 2016-10-09 11:05:01
Hi Rehan, I follow many of your post and work, great job man! thanks for helping the community!
Curious to know if you have something to automate things like these:
[Route("TermsOfUse", Name= "TermsOfUse")] [Route("Home/TermsOfUse", Name = "HomeTermsOfUse")] public ActionResult TermsOfUse() { if (Request.GetDisplayUrl().Contains("/Home")) return RedirectToRoutePermanent("TermsOfUse"); return View(); } [Route("Privacy", Name= "Privacy")] [Route("Home/Privacy", Name= "HomePrivacy")] public ActionResult Privacy() { if (Request.GetDisplayUrl().Contains("/Home")) return RedirectToRoutePermanent("Privacy"); return View(); }
Looks like you want to the response Rewrite middleware.
Doron Grinzaig commented on 2016-11-29 20:05:44
Fantastic post - great that you shown options in mvc5 and mvc6
Just got here back because of inbound links report. First, thank you for promoting our website on your blog! Second, I must ask, aren't you afraid it will cause spam? Lastly, (since we had massive spam bots recently) why aren't you using recpatcha? We got dozens of Viagra spam messages!
Doron Grinzaig commented on 2016-11-29 20:10:03
Looks like you want to the response Rewrite middleware.
(I accidentally replied to a wrong comment, not sure how it happened. Sorry.)
Just got here back because of inbound links report. First, thank you for promoting our website on your blog! Second, I must ask, aren't you afraid it will cause spam? Lastly, (since we had massive spam bots recently) why aren't you using recpatcha? We got dozens of Viagra spam messages!
Sandeep commented on 2016-12-01 05:45:15
http://example.com/ and http://www.example.com/ should resolve to the same URL, but currently do not. please help me how to solve this error..(mvc 4 application)
Muhammad Rehan Saeed commented on 2016-12-05 13:41:29
http://example.com/ and http://www.example.com/ should resolve to the same URL, but currently do not. please help me how to solve this error..(mvc 4 application)
You usually do this using your host name providers services.
Travis commented on 2017-01-09 00:15:42
Any idea on how to add this to Asp.net Core 1.1.0? Best I can figure is it needs to be added in the services.AddMvc options set, but how do I add the routes items options? (Doesn't seem routetable.routes is valid)
Thanks!
Muhammad Rehan Saeed commented on 2017-01-09 16:08:44
Any idea on how to add this to Asp.net Core 1.1.0? Best I can figure is it needs to be added in the services.AddMvc options set, but how do I add the routes items options? (Doesn't seem routetable.routes is valid)
Thanks!
The ASP.NET Core Boilerplate project template uses it. If you create a project from the template, you can see how to use it.
Dima commented on 2017-05-31 12:23:23
Thanks Rehan,
it's a great article from urlhelper to filters, just some customization following my needs and it works perfect. I tested my site with different SEO plugins and result is awesome.
pooria commented on 2017-10-05 17:00:31
hi rehan it's a great work you are doing here i read and excute Successfully both post "dynamic site map " and "dynamic robot.txt" and i have xml sitemap in http://localhost:51515/home/SitemapXml but i want to now how serch engine now they must go to http://localhost:51515/home/SitemapXml not http://localhost:51515/sitemap.xml Thanks
Muhammad Rehan Saeed commented on 2017-10-06 10:29:31
hi rehan it's a great work you are doing here i read and excute Successfully both post "dynamic site map " and "dynamic robot.txt" and i have xml sitemap in http://localhost:51515/home/SitemapXml but i want to now how serch engine now they must go to http://localhost:51515/home/SitemapXml not http://localhost:51515/sitemap.xml Thanks
Google and Bing both have web pages where you can enter the URL to your sitemap.
Brad Landis commented on 2018-04-18 19:02:44
Great post! A change I would recommend is to ignore ChildAction, otherwise, if a parent has one of the Ignore attributes (such as NoLowercaseQueryStringAttribute), the ChildAction might try to redirect, which results in a page error.
Around line 83, insert this:
if (filterContext.IsChildAction) {
filterContext.Controller.ViewBag.CanonicalUrl = filterContext.ParentActionViewContext.Controller.ViewBag.CanonicalUrl;
return;
}
Muhammad Rehan Saeed commented on 2018-04-19 09:19:31
Great post! A change I would recommend is to ignore ChildAction, otherwise, if a parent has one of the Ignore attributes (such as NoLowercaseQueryStringAttribute), the ChildAction might try to redirect, which results in a page error.
Around line 83, insert this:
if (filterContext.IsChildAction) { filterContext.Controller.ViewBag.CanonicalUrl = filterContext.ParentActionViewContext.Controller.ViewBag.CanonicalUrl; return; }
Yes, I actually got a PR in the ASP.NET Core Boilerplate Framework project that does just this. I just hadn't updated the blog post.
Hooman commented on 2019-07-08 03:42:44
Hi, thanks for the great post. So, if I do all the redirects, then I don't need to add any canonical links to my web pages? The reason ask is that I have heard that its best practice to always add canonical URL to our pages (though I have not really seen an official document to back this up, hence the question)