Home > DeveloperSection > Forums > How to make clean url in mvc 4?
Anurag Sharma
Anurag Sharma

Total Post:40

Points:280
Posted on    February-21-2015 6:07 AM

 ASP.NET MVC Url Routing  Routing 
Ratings:


 1 Reply(s)
 915  View(s)
Rate this:

Hi I’m want to create simple blog for clean url I like to use routing in my app.

I added to RouteConfig.cs this code:

public class RouteConfig

        {

            public static void RegisterRoutes(RouteCollection routes)

            {

                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

 

                routes.MapRoute(

                    name: "Default",

                    url: "{controller}/{action}/{id}",

                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

                );

 

                routes.MapRoute(

                    name: "ArticleList",

                    url: "Articles/{category}/{page}",

                    defaults: new

                    {

                        controller = "Articles",

                        category = UrlParameter.Optional,

                        page = 1

                    });

            }

        }

 

And if I write in web browser url:

http://localhost:6666/Articles/SomeCategory/3

I want to move to this controller:

public class ArticlesController : ControllerBase<IHomeService>

        {

            public ActionResult Index(string category, int page = 0)

            {

                return View("~/Views/Article/Articles.cshtml");

            }

 

        }

 

with parameters category = "SomeCategory" and page = 1.

All I recieve is Server Error in '/' Application. The resource cannot be found.

What is wrong?



David Miller

Total Post:30

Points:210
Posted on    February-21-2015 6:27 AM

Hi try this code:

routes.MapRoute(

            name: "ArticleList",

            url: "{controller}/{category}/{page}",

            defaults: new

            {

                category = UrlParameter.Optional,

                page = 1,

                action = "Index"

            },

            constraints: new

            {

                controller = "Articles"

            }

       );

 

 

            routes.MapRoute(

                name: "Default",

                url: "{controller}/{action}/{id}",

                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

            );


Don't want to miss updates? Please click the below button!

Follow MindStick