为mvc3 webapplication自动生成规范

我想在我的网站上使用规范url。 我在互联网上阅读了一些关于它的内容,但我正在寻找一种解决方案,它将自动为我的运行时生成规范并将其添加到返回浏览器的html代码中。

我已经在互联网上找到了一个使用属性的例子,但这不是我想要的。 使用属性我还在决定哪个页面应该是规范或不是我自己,我希望每个页面都有一个自动生成的页面。 我认为应该有(现有的)解决方案吗? 我正在努力寻找一个好的榜样,所以任何帮助都会受到赞赏。

对于剃刀:

我为HtmlHelper做了一个扩展方法:

 public static MvcHtmlString CanonicalUrl(this HtmlHelper html, string path) { if (String.IsNullOrWhiteSpace(path)) { var rawUrl = html.ViewContext.RequestContext.HttpContext.Request.Url; path = String.Format("{0}://{1}{2}", rawUrl.Scheme, rawUrl.Host, rawUrl.AbsolutePath); } path = path.ToLower(); if (path.Count(c => c == '/') > 3) { path = path.TrimEnd('/'); } if (path.EndsWith("/index")) { path = path.Substring(0, path.Length - 6); } var canonical = new TagBuilder("link"); canonical.MergeAttribute("rel", "canonical"); canonical.MergeAttribute("href", path); return new MvcHtmlString(canonical.ToString(TagRenderMode.SelfClosing)); } 

获取当前URL

 public static MvcHtmlString CanonicalUrl(this HtmlHelper html) { var rawUrl = html.ViewContext.RequestContext.HttpContext.Request.Url; return CanonicalUrl(html, String.Format("{0}://{1}{2}", rawUrl.Scheme, rawUrl.Host, rawUrl.AbsolutePath)); } 

致电Razor View:

 @Html.CanonicalUrl() 

MVC 5具有为您的路由生成小写URL的新选项。 我的路线配置如下所示:

 public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { // Imprive SEO by stopping duplicate URL's due to case or trailing slashes. routes.AppendTrailingSlash = true; routes.LowercaseUrls = true; routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } } 

使用此代码,您不再需要规范化URL,因为这是为您完成的。 如果您使用HTTP和HTTPS URL并且想要一个规范URL,则可能会出现唯一的问题。 在这种情况下,使用上述方法并使用HTTPS替换HTTP非常容易,反之亦然。

接受的答案,虽然它提供了一个生成Canonical url的好方法。

它是射击自己的捷径!

它完全破坏了使用规范标签的意义!

为什么规范标签存在?

当Google抓取您的网站并发现重复内容会对您造成不利影响。

您可以通过各种途径访问您网站中的同一页面。

 http://yourdomain.com/en https://yourClientIdAt.YourHostingPacket.com/ http://195.287.xxx.xxx //Your server Ip https://yourdomain.com/index http://www.yourdomain.com/ http://www.yourdomain.com/index .....etc... etc.. 

Google会在各种路径中找到相同的内容,从而重复内容,从而受到惩罚。

虽然最佳做法是使用301重定向,并且只有1个链接指向相同的网页,这是一个痛苦的……

这就是为什么创建了rel =“canonical”的原因。 它是一种告诉爬虫的方法

“嘿,这不是一个不同的页面,这是你之前搜索过的www.mydomain.index页面….规范标签中的链接是正确的!”

然后,同一网页不会作为不同的网页多次抓取。

通过从url动态生成规范链接,您只是说….

是….这是一个不同的页面抓取这个也…. 这是一个不同的……这一个……

因此,为了拥有一个正常工作的标准标记,您必须为每个具有不同内容的页面生成相同的完全链接。 决定您的主域名 (www.etc.com), 协议 (Https / Http)和Letter Casing(/索引,/索引),并生成唯一标识单个页面的链接。 这是你的控制器 / 动作 (也许是语言 )组合。 因此,您可以从路径数据中提取这些值。

 public static TagBuilder GetCanonicalUrl(RouteData route,String host,string protocol) { //These rely on the convention that all your links will be lowercase! string actionName = route.Values["action"].ToString().ToLower(); string controllerName = route.Values["controller"].ToString().ToLower(); //If your app is multilanguage and your route contains a language parameter then lowercase it also to prevent EN/en/ etc.... //string language = route.Values["language"].ToString().ToLower(); string finalUrl = String.Format("{0}://{1}/{2}/{3}/{4}", protocol, host, language, controllerName, actionName); var canonical = new TagBuilder("link"); canonical.MergeAttribute("href", finalUrl); canonical.MergeAttribute("rel", "canonical"); return canonical; } 

为了让你的HtmlHelper能够与你的约会产生一致的链接@Muhammad Rehan Saeed回答了这个问题。

然后,为了生成所有页面的Canonical标签,您必须制作HtmlHelper扩展

 public static MvcHtmlString CanonicalUrl(this HtmlHelper html,string host,string protocol) { var canonical = GetCanonicalUrl(HttpContext.Current.Request.RequestContext.RouteData,host,protocol); return new MvcHtmlString(canonical.ToString(TagRenderMode.SelfClosing)); } @Html.CanonicalUrl("www.mydomain.com", "https"); 

或者为控制器实现动作filter属性。 (我使用这种方法来处理同一个应用程序上的多个域等更复杂的场景……)

  public class CanonicalUrl : ActionFilterAttribute { private string _protocol; private string _host; public CanonicalUrl(string host, string protocol) { this._host = host; this._protocol = protocol; } public override void OnResultExecuting(ResultExecutingContext filterContext) { var canonical = GetCanonicalUrl(filterContext.RouteData,_host,_protocol); filterContext.Controller.ViewBag.CanonicalUrl = canonical.ToString(); } } } 

控制器内的用法

 [CanonicalUrl("www.yourdomain.com","https")] public class MyController : Controller 

然后我在_Layout.chtml上使用它并完成了!

 @Html.Raw(ViewBag.CanonicalUrl) 

问题解决了。 修复了它编写我自己的html帮助程序,它通过从请求中获取URL来生成规范URL。 这是否使用了此主题的信息。