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 ...
随机推荐
- ie无法下载 无法打开该internet站点.请求的站点不可用或无法找到.请稍后重试
PK找的: http://support.microsoft.com/kb/316431/ 症状 ... 如果服务器使用着安全套接字层 (SSL) 并且已将下面的两个 HTTP 标头或其中的一个添加到 ...
- js访sleep方法
function sleep(n) { var start = new Date().getTime(); while (true) if (new Date().getTime() - start ...
- Could not find class XXX referenced from method XXX.<YYY>
Since some ADT-Version you have to set which libraries / projects should be exported too. Project-Pr ...
- Map小记
Corelocation地理定位Map Kit地图展示 在移动互联网的时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边找餐馆找酒店,找银行,找电影院在上述应用中都用到了地图 ...
- Android --ListView分页
参考博客:Android ListView分页加载(服务端+android端)Demo 监听OnScrollListener事件 class OnListScrollListener implemen ...
- Springfox Reference Documentation
1. Introduction The Springfox suite of java libraries are all about automating the generation of mac ...
- mysql在线改表结构 pt-online-schema-change
https://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html 不锁表更改数据库表结构 pt-online-s ...
- python AES 双向对称加密解密
高级加密标准(Advanced Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...
- 修改最大打开文件数和最大proc数量
1.vim /etc/profile 增加 ulimit -n 10240 ulimit -u 10240 2.修改/etc/security/limits.conf * soft ...
- PostgreSQL Replication之第十一章 使用Skytools(5)
11.5 关于walmgr 的介绍 walmgr 是一个简化基于文件事务日志传输的工具.早在过去的一些日子里(在9.0版本之前),使用walmgr来简化基本备份是很常见的.随着流复制的引入,情况有了一 ...