1

I am new to ASP.NET-MVC and I am trying to create a simple blog application. I want to use a custom url for the blog's details pages.

Right now the url for blog's details pages are the standard 'localhost/Blog/Details/3', but I want to actually use the url 'localhost/Blog/2012/06/blog-title', basically using 'localhost/Blog/{year}/{month}/{BlogTitle}'

I have tried looking on the internet but I do not understand how to do this and am not able to get a simple tutorial on how either.

2 Answers 2

2

You can create a new route in Global.asax.cs as below,

routes.MapRoute(
   "Post",                                         // route-name
   "Blog/{year}/{month}/{BlogTitle}",              // format
   new { controller = "Books", action = "Post" },  // controller & action
   new { year = @"\d{4}", month = @"\d{2}" }       // constraints
);
Sign up to request clarification or add additional context in comments.

Comments

2

You have to map a custom route

routes.MapRoute(
                "Default", // Route name
                "Blog/{action}/{month}/{BlogTitle}", // URL with parameters
                new {controller ="MyController"}

            );

Any url of type localhost/Blog/text/text/text will map to this route

this url will call MyController.Action(month,BlogTitle)

Make sure to put the more restrictive routes first becouse the first route that matches the url will be considered (from top to bottom)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.