0

I'm making a blog in mvc 4 and trying make a route .

Here is how it looks now:

/About/Index

But I'd like it to look like:

/About/Firstname-Lastname

The firstname and lastname is always the same; it's not supposed to be dynamic. Here is what I have so far. I know it is because it needs to know what view show. So is there a way to say if /About/Firstname-Lastname then show Index?

routes.MapRoute(
            name: "About",
            url: "{controller}/{name}/",
            defaults:
                new
                    {
                        controler = "About",
                        action =  UrlParameter.Optional,
                        name = "firstname-Lastname"

                    }
            );
1
  • I don't believe the action can be optional. The route has to know which method on the controller to invoke. Commented Jul 31, 2013 at 19:34

2 Answers 2

4

This should do the trick

routes.MapRoute(
    name: "About",
    url: "About/Anne-Refsgaard/",
    defaults:
        new
            {
                controler = "About",
                action =  "Index",
            }
    );

This route needs to be added before the standard route

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

4 Comments

Looks like you were faster =)
Have allready tried that but just tried it again to be 100% sure i still get 404 on /About/First-Name but i hit when i request /About/Index
@MikkelRefsgaard: this route need to be added BEFORE default route. Have you done that?
@Andre Calil: It happens to all of us ;)
2
routes.MapRoute(
                name: "FirstnameLastname",
                url: "about/Andre-Calil",
                defaults: new { controller = "About", action = "Index" }
            );

Remember that the order of the routes matter. The first route that accepts the request will be used.

2 Comments

@ChadRuppert There is no parameter, OP stated that Firstname-Lastname are fixed values. OP simply wants to modify the URL, after all. This is no joke answer.
yeah, i removed my comment after I noticed that. and upvoted instead of down

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.