转自:http://www.ehsanghanbari.com/Post/20/how-to-create-urlslug-in-aspnet-mvc

UrlSlug Is a way of generating a valid Url, and using the title of an article to generate a URL. UrlSlug is very important in CEO because google likes to index the meaningful Urls at the first and then it refers to other Urls. Spouse you wanna to create the this Url:

  1. MyWebSite.com/Blog/Post/2013/4/14/how-to-create-url-slug-in-aspnet-mvc

create the model class :

public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public string PostSlug { get; set; }
public DateTime CreationTime { get; set; }
}

Now to creating the UrlSlud you have to call a function, you can create it as an extension method like this:

public static class SlugGeneratorHelper
{
public static string GenerateSlug(
this string phrase, int maxLength = 100)
{
string str = phrase.ToLower();
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
str = Regex.Replace(str, @"[\s-]+", " ").Trim();
str = str.Substring(0, str.Length <= maxLength ?
str.Length : maxLength).Trim();
str = Regex.Replace(str, @"\s", "-");
return str;
}
}

Now it's time to use this extension method, create the CreatePost action and use the GenerateSlug

public ActionResult CreatePost()
{
return View("CreatePost");
} [HttpPost]
public ActionResult CreatePost(Blog blog)
{
if (ModelState.IsValid)
{
_blogService.CreateBlogPost(blog);
blog.PostSlug = blog.Title.GenerateSlug();
}
return View("CreatePost");
}

You craeted the postSlug, now about how to use and show it in URL look at the action below

public ActionResult Post(int year, int month, int day, string postSlug)
{
var post = _blogService.GetBlogPostByDate(year,month,day,postSlug);
return View("Post", post);
}

GetBlogPostByDate is a method that you can define in your repository to get the post by year, month , day and postSlug ; something like this:

public Blog GetBlogPostByDate (int year, int month, int day,string postSlug)
{
var query =
_dbContextConfiguration.Blog.Where(
p => p.CreationTime.Year == year && p.CreationTime.Month == month && p.CreationTime.Day == day&&p.PostSlug==postSlug); return query.Single();
}

Finally register this route in your global.

asax

 routes.MapRoute("BlogRoute",
"Post/{year}/{month}/{day}/{postSlug}",
new
{
controller = "Blog",
action = "Post",
year = UrlParameter.Optional,
month = UrlParameter.Optional,
day = UrlParameter.Optional,
newsSlug = ""
},
new[] { "SampleProject.Web.Mvc.UI.Controllers" });

You have done, test it!

How to create UrlSlug in Asp.Net MVC的更多相关文章

  1. ASP.NET MVC 从零开始 - 自动化部署(其二)

    这篇文章是从我的 github 博客 http://lxconan.github.io 导入的. 这是这个系列的第五篇了,前四篇请参见: ASP.NET MVC 从零开始 – Create and R ...

  2. ASP.NET MVC 从零开始 - 请求处理

    这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...

  3. ASP.NET MVC 从零开始 - 自动化部署(其一)

    本文是从我的 github 博客 http://lxconan.github.io 导入的. 这是这个系列的第四篇了,前三篇请参见: ASP.NET MVC 从零开始 – Create and Run ...

  4. [webgrid] – selecterow - (Get Selected Row from ASP.NET MVC 3 WebGrid)

    Get Selected Row from ASP.NET MVC 3 WebGrid Abstract: The following article demonstrates how to get ...

  5. Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 1

    Your ASP.NET MVC application needs reports. What do you do? In this article, I will demonstrate how ...

  6. Using the Repository Pattern with ASP.NET MVC and Entity Framework

    原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...

  7. ASP.NET MVC 简单分页代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery

    本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...

  9. [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework

    本文转自:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-a ...

随机推荐

  1. Android ListView 自定义 Adapter

    自定义Adapter类 public class ListViewAdapter extends BaseAdapter { private static final String TAG = Mai ...

  2. Linux系统 ssh图形界面远程

    远程Linux系统有图形界面 1.下载xming 并安装启动 2.通过putty登陆虚拟机 3.输入gnome-session

  3. Bugfree实用心得_转

    转自:http://blog.csdn.net/benkaoya/article/details/8719257 本博下有许多实用技巧 1. 什么是问题跟踪系统 问题跟踪系统(Issue Tracki ...

  4. myeclipse WIN7下设置字体列表中无Courier New

    到"C:\Windows\Fonts"下找到对应的字体,单击,选择上面的显示 然后就可以在eclicpse字体设置里面看到Courier New项了: Eclipse字体设置方法: ...

  5. 浅谈C# 匿名变量

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  6. 客户端 ios与android 的判断

    <script> if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //alert(navigator.userAgen ...

  7. Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-1

    学习目标 一.进一步学习Swift的游戏制作 二.掌握SKNode,SKSpriteNode的运用 三.了解SpriteKit的物理系统 四.掌握动作(SKAction)的运用 在这一章,我们要通过制 ...

  8. Lintcode: Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  9. yii框架中保存第三方登录信息

    (第三方登录) 创建应用,域名,详情请看:http://www.cnblogs.com/xujn/p/5287157.html 效果图:

  10. ef 5 在 DropCreateDatabaseAlways 报错,the connection is currently used

    go sp_who2 -- db_id 数据库名称,查询出来的结果执行一遍就能关闭所有连接 SELECT N'kill '+ CAST(spid AS varchar) FROM master..sy ...