简体   繁体   English

我可以隐藏路线的一部分吗?

[英]Can I hide a part of a route?

I have an AppController and an AccountController. 我有一个AppController和一个AccountController。 The AppController only has one view, index , which takes query string parameters from the id part of the url. AppController只有一个视图index ,它从url的id部分获取查询字符串参数。

The default route is as follows: {controller}/{action}/{id} This means for the query string parameters to work properly, the view name has to be in the url. 默认路由如下: {controller}/{action}/{id}这意味着要使查询字符串参数正常工作,视图名称必须位于url中。 url/view/id

I would like to hide that part of the url and render that view by default, so users need only go to url/id . 我想隐藏url的那部分并默认渲染该视图,因此用户只需要转到url/id

I have tried {controller}/{id} and {controller}/index/{id} but neither work. 我试过{controller}/{id}{controller}/index/{id}但都没有工作。

I think this would work. 我认为这会奏效。 Set the url as : "{controller}/{id}" and give it a default action parameter: 将网址设置为: "{controller}/{id}"并为其指定默认操作参数:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

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

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