Tripping
Tripping

Reputation: 919

custom url in asp .net mvc

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.

Upvotes: 1

Views: 359

Answers (2)

VJAI
VJAI

Reputation: 32768

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
);

Upvotes: 2

Rzv.im
Rzv.im

Reputation: 1028

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)

Upvotes: 2

Related Questions