码迷,mamicode.com
首页 > Web开发 > 详细

How to create UrlSlug in Asp.Net MVC

时间:2014-05-26 14:02:12      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

转自: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 :

bubuko.com,布布扣
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; } 
}
bubuko.com,布布扣

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

bubuko.com,布布扣
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; 
    } 
}
bubuko.com,布布扣

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

bubuko.com,布布扣
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");
        }
bubuko.com,布布扣

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

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

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

bubuko.com,布布扣
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(); 
        }
bubuko.com,布布扣

Finally register this route in your global.

bubuko.com,布布扣
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" });
bubuko.com,布布扣

You have done, test it!

How to create UrlSlug in Asp.Net MVC,布布扣,bubuko.com

How to create UrlSlug in Asp.Net MVC

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/hsuyafeng/p/3746560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!