我在控制器中有一个方法ApplicationsController
,我需要在其中获取操作方法的基本 URL:
public ActionResult MyAction(string id)
{
var url = Url.Action("MyAction", "Applications");
...
}
问题是这包括string id
来自当前路由数据的,当我需要没有 URL 时(该 URL 用于在基于 URL 的查找中从 CMS 获取内容)。
我试过传递null
和new { }
作为routeValues
参数无济于事。
匹配路线如下(高于所有其他路线):
routes.MapLowercaseRoute(
name: "Applications",
url: "applications/{action}/{id}",
defaults: new { controller = "Applications",
action = "Index", id = UrlParameter.Optional });
我已经看到其他几个问题涉及此问题,但似乎都没有可行的解决方案。目前,我正在诉诸于对控制器中的路径进行硬编码;但是,我希望能够将其抽象为动作过滤器,因此我需要能够生成 URL。
有没有一种干净/传统的方法来防止这种行为?