Implement SEO Friendly URL in Asp.net MVC with Database functionality

SEO based URL commonly used in modern web applications. Asp.net MVC provide us facility to achieve this functionality. This tutorial will show you how to save URL in database and then make your request on the base of these URL.

Database Design

Now first we create our Database tables. one is RouteTable and second is BlogEntry table.
RouteTable Table Design
SEO URL in asp.net mvc
BlogEntry Table Design
SEO URL in asp.net mvc

RouteTable Script

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[RouteTable](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[LinkText] [varchar](50) NOT NULL,
	[URL] [varchar](max) NOT NULL,
 CONSTRAINT [PK_RouteTable] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

BlogEntry Table Script

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[BlogEntry](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[URLID] [int] NOT NULL,
	[Descrption] [varchar](50) NOT NULL,
 CONSTRAINT [PK_BlogEntry] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[BlogEntry]  WITH CHECK ADD  CONSTRAINT [FK_BlogEntry_RouteTable] FOREIGN KEY([URLID])
REFERENCES [dbo].[RouteTable] ([id])
GO

ALTER TABLE [dbo].[BlogEntry] CHECK CONSTRAINT [FK_BlogEntry_RouteTable]
GO

Now Create a new project in visual studio and make a simple ActionController name it PagesLinks and add its corresponding view.

RouteConfig Changes

You need to Register a new route in RouteConfig file. open this file from App_Start folder->RouteConfig.cs

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{FriendlyURL}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, FriendlyURL = "" }
            );


        }

i used entity framework with database first approach. you can learn it by Reading this article.

PagesLinks Controller code

 public ActionResult PagesLinks()
        {
            pubsEntities dbcontext=new pubsEntities();
            IQueryable GetArticleLinks =(from link in dbcontext.RouteTables select link);
            return View(GetArticleLinks);
        }

PagesLinks View code

@model IEnumerable
@{
    ViewBag.Title = "PagesLinks";
}

@foreach (var item in Model)
{
@Html.ActionLink(item.LinkText, "ShowBlogDetail", new { id = item.id, FriendlyURL = item.URL }) <br/>
}

No we create a new controller name it ShowBlogDetail

ShowBlogDetail Controller code

public ActionResult ShowBlogDetail()
        {
            int BlogID= int.Parse(RouteData.Values["id"].ToString());
            pubsEntities dbcontext=new pubsEntities();
            IQueryable GetBlogDetails=(from blog in dbcontext.BlogEntries where blog.URLID==BlogID select blog);
            return View(GetBlogDetails);
        }

ShowBlogDetail View code

@model IEnumerable
@{
    ViewBag.Title = "ShowBlogDetail";
}
@foreach (var item in Model)
{
    <p>@item.Descrption</p>
}