0

I've just started working on MVC 3 and i want to create a url like 'http://server/news/9635/demo-news-title' but i don't know how can i map routes.

routes.MapRoute(
                "news",
                "{controller}/{id}/{title}",
                new { controller = "news", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
                );

i tried this, but it doesn't seem to be working. What should i do?

Thanks

3
  • What is 'not working'? are you getting an exception? finding the wrong controller? what specifically isn't happening that you expect to be? Commented Dec 13, 2011 at 10:47
  • well when i wrote '/news/221/demo-title' to the browser i got a "Server Error in / Application" error. Is the mapRoute method good for the url i wrote above? Commented Dec 13, 2011 at 10:49
  • can you show us your controller action? Commented Dec 13, 2011 at 23:13

3 Answers 3

2

You provided no information about what you mean with "not working". To check if your route is the problem you can use Phil Haacks's ASP.NET Routing Debugger

Sign up to request clarification or add additional context in comments.

1 Comment

well i thing i solve the problem, i got 2 maproutes, one for default and one for the news, so i changed the order of them and now it seems working...
1

I think your route is right, but you have to put your route before default route . This is example :

routes.MapRoute(
                "news",
                "{controller}/{id}/{title}",
                new { controller = "news", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
                );
routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

Comments

0

Do it this way:

routes.MapRoute(
            "news",
            "news/{id}/{title}",
            new { controller = "news", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
            );

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.