ASP.NET MVC单向路由 [英] ASP.NET MVC One Way Route

查看:62
本文介绍了ASP.NET MVC单向路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Asp.net MVC的RouteCollection中定义一条路由,以便仅执行 URL重写部分,而忽略Html.Actionlink(...)生成的URL?

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(...)?

事实上,我想为某些非常特殊的请求在控制器和动作之间添加一个关键字(controller / .. keyword ... / action)。但是,页面上生成的URL应该使用默认路由保留。 (控制器/操作)

In fact I want to add a keyword between controller and action (controller/..keyword.../action) for certain very special requests. The generated URLs on the pages, however, should remain using the default routes. (controller/action)

推荐答案

是的,您可以执行所要求的操作。您只需创建从当前路径继承的自己的路由,并覆盖 GetVirtualPath 以始终返回null。这样就不会执行任何操作查找,但是URL仍将充当到您的操作/控制器的路由映射。

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. This way no Action lookup would be done, but the URL would still function as a routing mapping to your action/controller.

此外,发生的事情是' URL重写,因为您使用路由在应用程序中定义了端点。换句话说,就是API。因此,不会进行任何重写。将路由和您的操作/控制器之间的关系看作是Web的公共定义的名称空间。就像命名空间一样,您正在应用程序中定义一个特定的位置,可以在其中找到您的动作/控制器。

Also by the way, what is happening isn't URL Rewriting, because you using the Routes to define endpoints in to your application. Or in other words an 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. Just like a namespace you are defining a specific spot in your application where your action/controller can be found.

这篇关于ASP.NET MVC单向路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆