Mikkel Refsgaard
Mikkel Refsgaard

Reputation: 111

Asp.NET Mvc 4 Routing

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"

                    }
            );

Upvotes: 0

Views: 87

Answers (2)

Claudio Redi
Claudio Redi

Reputation: 68440

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

Upvotes: 4

Andre Calil
Andre Calil

Reputation: 7692

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.

Upvotes: 2

Related Questions