简体   繁体   English

ASP.NET MVC单向路由

[英]ASP.NET MVC One Way Route

Is it possible to define a route in the RouteCollection of Asp.net MVC, so that it just does the "URL rewriting" part and ignore the URL generation with Html.Actionlink(...)? 是否可以在Asp.net MVC的RouteCollection中定义路由,以便它只执行“URL重写”部分并忽略使用Html.Actionlink(...)生成URL?

In fact I want to add a keyword between controller and action (controller/..keyword.../action) for certain very special requests. 事实上,我想在控制器和动作之间添加一个关键字(controller / .. keyword ... / action),用于某些非常特殊的请求。 The generated URLs on the pages, however, should remain using the default routes. 但是,页面上生成的URL应保留使用默认路由。 (controller/action) (控制器/作用)

Yes you can do what you are asking. 是的,你可以做你想要的。 You would just create your own route that inherited from the current one and override GetVirtualPath to always return null. 您只需创建自己的路由继承自当前路由并覆盖GetVirtualPath以始终返回null。 This way no Action lookup would be done, but the URL would still function as a routing mapping to your action/controller. 这样就不会进行Action查找,但URL仍然可以作为行动/控制器的路由映射。

Also by the way, what is happening isn't URL Rewriting, because you using the Routes to define endpoints in to your application. 顺便说一句,发生的事情不是URL重写,因为您使用Routes在应用程序中定义端点。 Or in other words an API. 或者换言之,API。 So no rewriting is taking place. 所以没有重写。 Think of the relationship between routes and your action/controller as more of a publically defined namespace for the web. 将路由与您的操作/控制器之间的关系想象为更多公共定义的Web命名空间。 Just like a namespace you are defining a specific spot in your application where your action/controller can be found. 就像命名空间一样,您可以在应用程序中定义可以找到操作/控制器的特定位置。

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

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