转自: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. 网站提供的下载IE8很慢 由于Microsoft 联机服务暂时不可用,SmartScreen筛选器无法检查此网站。

    在内网环境中,网站系统提供了一个下载功能,用ie8下载特别慢,一个20kb的文件,下载要10分钟,但是在其他环境中是很快的,试了半天,原来是:由于Microsoft 联机服务暂时不可用,SmartSc ...

  2. sqlite数据库 adb 从配置到查询表中数据全过程-----献给初学的自己

    1.   E:\Android\android-sdk-windows\platform-tools[将adb.exe文件的路径放到path中,设置环境变量] 2.  adb -s emulator ...

  3. T4教程2 T4模版引擎之生成数据库实体类

    T4模版引擎之生成数据库实体类   在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实战篇.T4模板引擎可以当做一个代码生成器,代码生成器的职责当然是用来生成代码(这不是废话吗).而这其中 ...

  4. python enumerate

    >>> a = [1, 2, 3] >>> for index, item in enumerate(a): print index, item 0 1 1 2 2 ...

  5. DG - physical standby switchover切换过程

    一.切换前检查1.检查备库已经全部接收到主库的redo如果是最大可用性.最大保护性模式,可以在primary端查看v$archive_dest_status,确认是否所有的redo已经传送到备库#在主 ...

  6. Java基础之读文件——使用缓冲读取器读取文件(ReaderInputFromFile)

    控制台程序,本例读取Java基础之写文件部分(WriterOutputToFile)写入的Saying.txt. import java.io.*; import java.nio.file.*; i ...

  7. .NET业务实体类验证组件Fluent Validation

    认识Fluent Vaidation. 看到NopCommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真是一个天才的想法,后来才知道这个东西是一个开源的轻量级验证组建. ...

  8. 《30天自制操作系统》11_day_学习笔记

    harib08a: 鼠标的显示问题:我们可以看到,鼠标移到窗口最右侧之后就不能再移动了,而WIN中,鼠标是可以移动到最右边隐藏起来的.怎么办?把鼠标指针显示的范围扩宽就行!我们来修改一下HariMai ...

  9. CPU informition

    tar jxvf util-linux-ng-2.18.bz2cd util-linux-ng-2.18/./configure --enable-arch --enable-partx --enab ...

  10. override与final

    override 强调该函数是重写的父类的函数 final 指定该函数不能被重写 两者都是针对virtual 函数