2015-01-20 25 views
1

我需要向我的控制器添加一個名爲'event'的操作,但這是一個保留字。添加事件動作的最簡單方法是什麼?如何創建一個名爲'event'的控制器操作

如:

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

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

給它一個資本E會傷害嗎? – 2015-01-20 19:42:59

+0

是的,我們希望保留現有的網址。 – pixelmike 2015-01-20 19:45:44

+0

該URL將不區分大小寫我認爲... – 2015-01-20 19:46:28

回答

3

使用ActionName屬性

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

賓果!謝謝。只需添加返回視圖(「EntertainmentEvent」);因爲你無法創建一個名爲「event」的視圖。 – pixelmike 2015-01-20 19:50:14

+0

@ pixelmike - 是的,意見需要相應地命名,很好的抓住:) – 2015-01-20 19:51:37

0

,你可以做,在路由映射。只是RouteConfig添加路由entertainment/event和路線圖,以自定義action..lets說「randomAction」

1

雖然我會建議反對,這將編譯:

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

哦,這看起來很邪惡。我喜歡。 – pixelmike 2015-01-20 20:11:31

相關問題