How to create UrlSlug in Asp.Net MVC
2014-05-22 21:59
543 查看
转自: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 :
Now to creating the UrlSlud you have to call a function, you can create it as an extension method like this:
Now it's time to use this extension method, create the CreatePost action and use the GenerateSlug
You craeted the postSlug, now about how to use and show it in URL look at the action below
GetBlogPostByDate is a method that you can define in your repository to get the post by year, month , day and postSlug ; something like this:
Finally register this route in your global.
You have done, test it!
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 custom html helper in Asp.net MVC 3 or 4
- How to bootstrap two angular apps in one asp.net mvc application
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- [转]How can I get my webapp's base URL in ASP.NET MVC
- how to get all controller attribute action and parameter in asp.net mvc
- How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- 【引】How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- How to use HTML-5 data-* attributes in ASP.NET MVC
- How to create simple and advanced pivot tables in C# and ASP.NET
- How to achive the CRUD in ASP.NET MVC(VS2010)
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- How to get URL and QueryString value in an ASP.NET page
- ASP.NET MVC: how to include <span> in link from Html.ActionLink?
- How to Create a Second Level GridView in ASP.NET
- Using JQuery to perform Ajax calls in ASP.NET MVC
- How to use FtpWebRequest in asp.net
- How To contain multiple fileds in the querystrig, DataNavigateUrlFormatString=xxx.asp?ID={0}&Name={1}
- Using the Index Server to create Query Page in asp.net
- How to add controls dynamically in asp.net
- How to add icons to a SiteMap Menu in ASP .NET