如何创建名为 'event' 的控制器操作

How to create a controller action called 'event'

我需要向我的控制器添加一个名为 'event' 的操作,但这是一个保留字。添加事件操作的最简单方法是什么?

例如:

public class entertainmentController : Controller
{
    // GET: entertainment
    public ActionResult Index()
    {
        return View();
    }

    // GET: entertainment/event
    public ActionResult event()  // <-- won't compile
    {
        return View();
    }
}

你可以在路由映射中做到这一点。只需在 RouteConfig 中添加路由 entertainment/event 并将该路由映射到自定义操作..让我们说 'randomAction'

使用 ActionName 属性

// GET: entertainment/event
[ActionName("event")]
public ActionResult EntertainmentEvent()
{
    return View("EntertainmentEvent");
}

虽然我不建议这样做,但这将编译:

public ActionResult @event()
{
    return View();
}