How to create UrlSlug in Asp.Net MVC
转自: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:
- 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的更多相关文章
- ASP.NET MVC 从零开始 - 自动化部署(其二)
这篇文章是从我的 github 博客 http://lxconan.github.io 导入的. 这是这个系列的第五篇了,前四篇请参见: ASP.NET MVC 从零开始 – Create and R ...
- ASP.NET MVC 从零开始 - 请求处理
这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...
- ASP.NET MVC 从零开始 - 自动化部署(其一)
本文是从我的 github 博客 http://lxconan.github.io 导入的. 这是这个系列的第四篇了,前三篇请参见: ASP.NET MVC 从零开始 – Create and Run ...
- [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 ...
- 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 ...
- 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 ...
- ASP.NET MVC 简单分页代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery
本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...
- [转]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 ...
随机推荐
- [BS] 小知识点总结-02
1. dispatch_GCD 可让某操作延迟x秒执行 //模拟网速慢,延迟3s返回数据(就会导致右侧数据和左侧标签不对应) dispatch_after(dispatch_time(DISPATC ...
- Linux 内核版本规律
版本组成:主版本号.次版本号.修正版本号 主版本号和次版本号一起标志着重要的功能变更,修正版本号表示较小的功能变更.次版本号表示该版本是否为稳定版本,偶数则为稳定版本,奇数则可能存在一些BUG.
- Using Amazon API Gateway with microservices deployed on Amazon ECS
One convenient way to run microservices is to deploy them as Docker containers. Docker containers ar ...
- 《30天自制操作系统》04_day_学习笔记
harib01a: P65 用C语言实现内存写入 实现一个往黑画面上写入东西的函数 修改了naskfunc.nas中的内容 在bootpack.c中 用write_mem8()函数将VRMA中全部写入 ...
- python入门到精通[一]:搭建开发环境
摘要:Python认识,及在windows和linux上安装环境,测试是否安装成功. 1.写在前面 参加工作也有5年多了,一直在做.net开发,近一年有做NodeJS开发.从一开始的不习惯,到逐步适应 ...
- jquery垂直展开折叠手风琴二级菜单
摘要:jquery实现垂直展开二级菜单 最近新开发一个简单项目,用到左侧两级的菜单.找找了手头的文件,竟然没有现成的代码,算了,去网上找找整理下吧. 注:jquery-1.8.3.min.js需要下载 ...
- IOS推荐学习网站
1> 个人博客:技术大牛 唐巧:http://blog.devtang.com/blog/archives/ 王巍:http://www.onevcat.com 破船之家:http://beyo ...
- java中的BigDecimal和String的相互转换
/*由数字字符串构造BigDecimal的方法 02.*设置BigDecimal的小数位数的方法 03.*/ 04.import java.math.BigDecimal; 05.//数字字符串 06 ...
- [原创]java WEB学习笔记78:Hibernate学习之路---session概述,session缓存(hibernate 一级缓存),数据库的隔离级别,在 MySql 中设置隔离级别,在 Hibernate 中设置隔离级别
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- javascript 内置对象
什么是对象 javascript中的所有事物都是对象:字符串 数组 数值 函数... 每个对象都带有属性和方法 javascript允许自定义对象 自定义对象: 定义并创建对象实例 使 ...