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 ...
随机推荐
- 第一段nodejs代码
步骤一.创建服务器 接下来我们使用 http.createServer() 方法创建服务器,并使用 listen 方法绑定 8888 端口. 函数通过 request, response 参数来接收和 ...
- sqlite数据库 adb 从配置到查询表中数据全过程-----献给初学的自己
1. E:\Android\android-sdk-windows\platform-tools[将adb.exe文件的路径放到path中,设置环境变量] 2. adb -s emulator ...
- [BS-18] 对OC中不可变类的理解
对OC中不可变类的理解 OC中存在很多不可变的类(如NSString,NSAttributedString,NSArray,NSDictionary,NSSet等),用它们创建的对象存在于堆内存中,但 ...
- .Net Framework 4.5.2 on Windows 10
I was using Visual Studio 2013 to create a new solution, could not select ".NET Framework 4.5.2 ...
- css 字体样式
[强制] font-family 属性中的字体族名称应使用字体的英文 Family Name,其中如有空格,须放置在引号中. 解释: 所谓英文 Family Name,为字体文件的一个元数据,常见名称 ...
- leetcode-99 Recover_Binary_Search_Tree
题目要求: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without ch ...
- Leetcode: Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- hdu 2528 Area
2014-07-30 http://acm.hdu.edu.cn/showproblem.php?pid=2528解题思路: 求多边形被一条直线分成两部分的面积分别是多少.因为题目给的直线一定能把多边 ...
- 转:Jmeter之Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- codeforces 520 Two Buttons
http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...