بهینه سازی برنامههای وب ASP.NET برای موتورهای جستجو (SEO)
نویسنده: محسن نجف زاده
تاریخ: ۱۳۹۲/۰۷/۲۴ ۱۴:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
private const string SeparatorTitle = " - ";
private const int MaxLenghtTitle = 60;
public static string GeneratePageTitle(params string[] crumbs)
{
var title = "";
for (int i = 0; i < crumbs.Length; i++)
{
title += string.Format
(
"{0}{1}",
crumbs[i],
(i < crumbs.Length - 1) ? SeparatorTitle : string.Empty
);
}
title = title.Substring(0, title.Length <= MaxLenghtTitle ? title.Length : MaxLenghtTitle).Trim();
return title;
} public enum CacheControlType
{
[Description("public")]
_public,
[Description("private")]
_private,
[Description("no-cache")]
_nocache,
[Description("no-store")]
_nostore
}
private const int MaxLenghtTitle = 60;
private const int MaxLenghtDescription = 170;
private const string FaviconPath = "~/cdn/ui/favicon.ico";
public static string GenerateMetaTag(string title, string description, bool allowIndexPage, bool allowFollowLinks, string author = "", string lastmodified = "", string expires = "never", string language = "fa", CacheControlType cacheControlType = CacheControlType._private)
{
title = title.Substring(0, title.Length <= MaxLenghtTitle ? title.Length : MaxLenghtTitle).Trim();
description = description.Substring(0, description.Length <= MaxLenghtDescription ? description.Length : MaxLenghtDescription).Trim();
var meta = "";
meta += string.Format("<title>{0}</title>\n", title);
meta += string.Format("<link rel=\"shortcut icon\" href=\"{0}\"/>\n", FaviconPath);
meta += string.Format("<meta http-equiv=\"content-language\" content=\"{0}\"/>\n", language);
meta += string.Format("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n");
meta += string.Format("<meta charset=\"utf-8\"/>\n");
meta += string.Format("<meta name=\"description\" content=\"{0}\"/>\n", description);
meta += string.Format("<meta http-equiv=\"Cache-control\" content=\"{0}\"/>\n", EnumExtensions.EnumHelper<CacheControlType>.GetEnumDescription(cacheControlType.ToString()));
meta += string.Format("<meta name=\"robots\" content=\"{0}, {1}\" />\n", allowIndexPage ? "index" : "noindex", allowFollowLinks ? "follow" : "nofollow");
meta += string.Format("<meta name=\"expires\" content=\"{0}\"/>\n", expires);
if (!string.IsNullOrEmpty(lastmodified))
meta += string.Format("<meta name=\"last-modified\" content=\"{0}\"/>\n", lastmodified);
if (!string.IsNullOrEmpty(author))
meta += string.Format("<meta name=\"author\" content=\"{0}\"/>\n", author);
//------------------------------------Google & Bing Doesn't Use Meta Keywords ...
//meta += string.Format("<meta name=\"keywords\" content=\"{0}\"/>\n", keywords);
return meta;
} private const int MaxLenghtSlug = 45;
public static string GenerateSlug(string title)
{
var slug = RemoveAccent(title).ToLower();
slug = Regex.Replace(slug, @"[^a-z0-9-\u0600-\u06FF]", "-");
slug = Regex.Replace(slug, @"\s+", "-").Trim();
slug = Regex.Replace(slug, @"-+", "-");
slug = slug.Substring(0, slug.Length <= MaxLenghtSlug ? slug.Length : MaxLenghtSlug).Trim();
return slug;
}
private static string RemoveAccent(string text)
{
var bytes = Encoding.GetEncoding("UTF-8").GetBytes(text);
return Encoding.UTF8.GetString(bytes);
} Head.InnerHtml = SEO.GenerateMetaTag
(
title: SEO.GeneratePageTitle(".NET Tips", "آرشیو مطالب", "ASP.NET MVC #1"),
description: "چرا ASP.NET MVC با وجود فریم ورک پختهای به نام ASP.NET web forms، اولین سؤالی که حین سوئیچ به ASP.NET MVC مطرح میشود این است: «برای چی؟». بنابراین تا به این سؤال پاسخ داده نشود، هر نوع بحث فنی در این مورد بی فایده است.",
allowIndexPage: true,
allowFollowLinks: true,
author: "وحید نصیری",
cacheControlType: SEO.CacheControlType._private
);
<title>.NET Tips - آرشیو مطالب - ASP.NET MVC #1</title> <link rel="shortcut icon" href="../../cdn/images/ui/favicon.ico"/> <meta http-equiv="content-language" content="fa"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <meta charset="utf-8"/> <meta name="description" content="چرا ASP.NET MVC ؟با وجود فریم ورک پختهای به نام ASP.NET web forms، اولین سؤالی که حین سوئیچ به ASP.NET MVC مطرح میشود این است: «برای چی؟». بن ..."/> <meta http-equiv="Cache-control" content="private"/> <meta name="robots" content="index, follow" /> <meta name="expires" content="never"/> <meta name="author" content="وحید نصیری"/>
Head.InnerHtml = SEO.GenerateMetaTag(....)
<head>
<asp:Literal runat="server" ID="litHead"></asp:Literal>
....
</head>
litHead.Text = SEO.GenerateMetaTag(...)
EnumExtensions.EnumHelper<CacheControlType>.GetEnumDescription(cacheControlType.ToString()));
Head.InnerHtml = SEO.GenerateMetaTag ....
- پروژه Iris هست. به section metatags اون دقت کن در فایلهای View.
- مثلا متد Head.InnerHtml = SEO.GenerateMetaTag عنوان شده در این مطلب باید در Page_Load یک وب فرم فراخوانی شود. Id مطلب رو دارید. عنوان و متن و سایر مشخصات اون رو از دیتابیس دریافت کنید و بعد فقط یک جایگذاری است در متد تهیه شده.
- به اینکار Routing و Url rewrite میگن. بحثش در MVC و وب فرمها کمی با هم فرق میکنه.
public enum CacheControlType
{
[Description("public")]
_public,
[Description("private")]
_private,
[Description("no-cache")]
_nocache,
[Description("no-store")]
_nostore
} Attribute 'Description' is not valid on this declaration type. It is only valid on 'method' declarations.
enum MyEnum {
[System.ComponentModel.Description("Blah")]
MyValue
}
تعداد نتایج یافت شده متفاوت این دو صفحه (با یک عنوان جستجو) نشان از تایید مطلب دارد.
// for Google var urlReferrer = Request.UrlReferrer.ToString(); var query = HttpUtility.ParseQueryString(urlReferrer); var searchQuery = query["q"];