为参数设置mvc路由
Setup mvc routing for parameter
我被要求设置路由以便下一条路由正常工作
https://<server>/<weapp>/<paramater>
我已经让默认控制器和动作正常工作,这条路线按预期工作
https://<server>/<weapp>/?param1=<paramater>
但是我的客户希望我摆脱 ?param1=
并且它仅通过在 url
末尾设置参数来按预期工作
我看过这个作品,但我就是找不到如何配置,也不知道为什么要搜索它的关键字。我的 google 傅在这方面很弱
所以,在潜伏在更晦涩的 google fu 发现之后,我终于找到了答案
我确实已经设置了一条路线,在我看来应该可以满足我的客户需求
routes.MapRoute(
name: "Shortener",
url: "{id}",
defaults: new { controller = "<controller>", action = "<action>", id = UrlParameter.Optional }//new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
但我总是在设置新的路线图之后设置新的路线图。我发现的这一点 in this article 提供了一些新的线索,我以前阅读的关于此事的文章从未提及,甚至 MSDN
One thing to keep in mind when designing your routes is that the order
in which the routes are added to the table matters. The routing engine
will take the first route that matches the supplied URL and attempt to
use the route values in that route. Therefore, less common or more
specialized routes should be added to the table first, while more
general routes should be added later on.
因此,在将我的路由配置设置在所有其他路由配置的顶部之后,url
https://<server>/<weapp>/<paramater>
工作得很好!
希望这对其他人有帮助!
我被要求设置路由以便下一条路由正常工作
https://<server>/<weapp>/<paramater>
我已经让默认控制器和动作正常工作,这条路线按预期工作
https://<server>/<weapp>/?param1=<paramater>
但是我的客户希望我摆脱 ?param1=
并且它仅通过在 url
我看过这个作品,但我就是找不到如何配置,也不知道为什么要搜索它的关键字。我的 google 傅在这方面很弱
所以,在潜伏在更晦涩的 google fu 发现之后,我终于找到了答案
我确实已经设置了一条路线,在我看来应该可以满足我的客户需求
routes.MapRoute(
name: "Shortener",
url: "{id}",
defaults: new { controller = "<controller>", action = "<action>", id = UrlParameter.Optional }//new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
但我总是在设置新的路线图之后设置新的路线图。我发现的这一点 in this article 提供了一些新的线索,我以前阅读的关于此事的文章从未提及,甚至 MSDN
One thing to keep in mind when designing your routes is that the order in which the routes are added to the table matters. The routing engine will take the first route that matches the supplied URL and attempt to use the route values in that route. Therefore, less common or more specialized routes should be added to the table first, while more general routes should be added later on.
因此,在将我的路由配置设置在所有其他路由配置的顶部之后,url
https://<server>/<weapp>/<paramater>
工作得很好!
希望这对其他人有帮助!