【问题标题】:How to set a controller method not to be routed using attribute routing如何设置不使用属性路由路由的控制器方法
【发布时间】:2014-04-20 04:36:21
【问题描述】:

我已在控制器级别应用了路由属性,但我想从路由中排除操作。不覆盖但完全排除路线。如何实现?

假设我有:

[RoutePrefix("promotions")]
[Route("{action=index}")]
public class ReviewsController : Controller
{
    // eg.: /promotions
    public ActionResult Index() { ... }

    // eg.: /promotions/archive
    public ActionResult Archive() { ... }

    // eg.: /promotions/new
    public ActionResult New() { ... }

    // eg.: /promotions/edit/5
    [Route("edit/{promoId:int}")]
    public ActionResult Edit(int promoId) { ... }

    public void Internal() { ... }
}

并且我希望内部不被路由。

我本来希望找到 [DoNotRoute] 或 [Ignore] 属性,但我没有找到类似的东西。

【问题讨论】:

  • 你是手动路由Internal,还是根本就不是动作?
  • 根本不是动作。它是一个公共方法,位于基本控制器及其接口的实现中。

标签: asp.net-mvc-routing asp.net-mvc-5 attributerouting


【解决方案1】:

使用[NonAction] 属性:

[NonAction]
public void Internal() { ... }

【讨论】:

  • 这个我试过了,但是没有效果。该操作仍显示在已注册路由列表中。
  • 试试这个网址,应该不行。您在哪里看到此已注册路线列表?
  • 索引、存档和新建的路径不一样吗?
  • 我已经检查过了,请求没有被路由到这个操作,这很好。我也希望不要将该方法列为路线,但我认为这只是“美学”问题。我仍然相信 DoNotRoute 属性将是一个更清洁的解决方案,与其他属性路由配置一样。或者,使用 [NonAction] 属性排除操作路线就足够了。
猜你喜欢
  • 2016-07-03
  • 1970-01-01
  • 2015-04-02
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2015-02-27
相关资源
最近更新 更多