Clean SEO Friendly URLs With ASP.NET MVC 3
2011-11-06 10:47
281 查看
Source:
http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC <---
http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls <----------
http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx <----------
http://stackoverflow.com/questions/3620092/asp-net-mvc-routing-seo-friendly-url
http://weblogs.asp.net/scottgu/archive/2010/04/20/tip-trick-fix-common-seo-problems-using-the-url-rewrite-extension.aspx
One of the major benefits of ASP.NET MVC is that it allows us truly dynamic URLs via its Routing system. We could declare a new Route like this:
routes.MapRoute(
"ArticleDetails", // Route name
"Article/{id}/{title}",
new { controller = "Article", action = "Details",title = "" },new { id = @"\d+" }
);[/b]
I wrote a simple method that encodes the URLs into a nice, safe, SEO friendly URL format:
public static class UrlEncoder
{
public static string ToFriendlyUrl(this UrlHelper helper, string title)
{
var url = title.Trim();
url = url.Replace(" ", " ").Replace(" - ", " ").Replace(" ", "-").Replace(",", "").Replace("...", "");
return url;
}
}[/b]
Here is some code on how you how could do this from your Controller Action:
public ViewResult Details(int id,string title)
{
Article article = db.Aritcles.Find(id);[/b]
string realTitle = UrlEncoder.ToFriendlyUrl(Url, article.Title).ToLower();
string urlTitle = (title ?? "").Trim().ToLower();
if (realTitle != urlTitle)
{
Response.Status = "301 Moved Permanently";
Response.StatusCode = 301;
Response.AddHeader("Location", "/Article/" + article.Id + "/" + realTitle);
Response.End();
}
return View(article);
}[/b]
Here is some code on how you how could do this from your View:
<h2>Article List</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr><th>Title</th><th>Content</th><th>CreateTime</th><th>Author</th><th></th></tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Title)</td>
<td>@Html.DisplayFor(modelItem => item.Content)</td>
<td>@Html.DisplayFor(modelItem => item.CreateTime)</td>
<td>@Html.DisplayFor(modelItem => item.Author)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id, title = Url.ToFriendlyUrl(item.Title) }) |
@Html.ActionLink("Details", "Details", new { id=item.Id ,title=Url.ToFriendlyUrl(item.Title)}) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>[/b]
Article List View:
Details View:
Download Source Code:http://www.kuaipan.cn/index.php?ac=file&oid=4876720616244129
http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC <---
http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls <----------
http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx <----------
http://stackoverflow.com/questions/3620092/asp-net-mvc-routing-seo-friendly-url
http://weblogs.asp.net/scottgu/archive/2010/04/20/tip-trick-fix-common-seo-problems-using-the-url-rewrite-extension.aspx
One of the major benefits of ASP.NET MVC is that it allows us truly dynamic URLs via its Routing system. We could declare a new Route like this:
routes.MapRoute(
"ArticleDetails", // Route name
"Article/{id}/{title}",
new { controller = "Article", action = "Details",title = "" },new { id = @"\d+" }
);[/b]
I wrote a simple method that encodes the URLs into a nice, safe, SEO friendly URL format:
public static class UrlEncoder
{
public static string ToFriendlyUrl(this UrlHelper helper, string title)
{
var url = title.Trim();
url = url.Replace(" ", " ").Replace(" - ", " ").Replace(" ", "-").Replace(",", "").Replace("...", "");
return url;
}
}[/b]
Here is some code on how you how could do this from your Controller Action:
public ViewResult Details(int id,string title)
{
Article article = db.Aritcles.Find(id);[/b]
string realTitle = UrlEncoder.ToFriendlyUrl(Url, article.Title).ToLower();
string urlTitle = (title ?? "").Trim().ToLower();
if (realTitle != urlTitle)
{
Response.Status = "301 Moved Permanently";
Response.StatusCode = 301;
Response.AddHeader("Location", "/Article/" + article.Id + "/" + realTitle);
Response.End();
}
return View(article);
}[/b]
Here is some code on how you how could do this from your View:
<h2>Article List</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr><th>Title</th><th>Content</th><th>CreateTime</th><th>Author</th><th></th></tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Title)</td>
<td>@Html.DisplayFor(modelItem => item.Content)</td>
<td>@Html.DisplayFor(modelItem => item.CreateTime)</td>
<td>@Html.DisplayFor(modelItem => item.Author)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id, title = Url.ToFriendlyUrl(item.Title) }) |
@Html.ActionLink("Details", "Details", new { id=item.Id ,title=Url.ToFriendlyUrl(item.Title)}) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>[/b]
Article List View:
Details View:
Download Source Code:http://www.kuaipan.cn/index.php?ac=file&oid=4876720616244129
相关文章推荐
- Creating a Clean, Minimal-Footprint ASP.NET WebAPI Project with VS 2012 and ASP.NET MVC 4
- Levenblog2.0.6 [With Asp.Net MVC 1.0]发布[build20090319]
- Asp.net MVC 4 application with Flexigrid, jQuery UI, and jQuery validation
- asp.net MVC中的AppendTrailingSlash以及LowercaseUrls
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- Asp.Net MVC 3【URLs, Routing,and Areas】续
- Ext JS With ASP.NET MVC Sample(1)
- jQuery UI Accordion in ASP.NET MVC - feed with data from database
- MvcPager 概述 MvcPager 分页示例 — 标准Ajax分页 对SEO进行优化的ajax分页 (支持asp.net mvc)
- ASP.NET MVC Routing、Areas、URLs
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- asp.net mvc combine with reporting service
- ASP.Net MVC upload file with record & validation - Step 6
- ASP.NET MVC 3: Layouts and Sections with Razor
- [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework
- jQuery Templates with ASP.NET MVC
- Chapter 10: Test Driven Development with ASP.NET MVC --- Professional ASP.NET MVC 1.0
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.12.ASP.NET MVC3的细节:URLs,Routing和Areas
- 谈谈asp.net MVC中的AppendTrailingSlash以及LowercaseUrls ,你还记得吗?
- 1.Getting Started with ASP.NET MVC 5