简体   繁体   English

Asp.NET Mvc 4路由

[英]Asp.NET Mvc 4 Routing

I'm making a blog in mvc 4 and trying make a route . 我正在MVC 4中创建博客,并尝试建立路由。

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? 那么有没有办法说/ About / Firstname-Lastname然后显示Index?

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

                    }
            );

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 需要在标准路由之前添加此路由

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. 将使用接受请求的第一条路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM