2013-12-17 55 views
0

我已經開始做ASP.NET MVC,但我不知道從哪裏開始解決這個問題。ASP.NET MVC路由入門

我已經創建了一個默認的應用程序,並且我創建了一個EventModel,EventController和一系列默認的事件視圖。這一切都正常工作。

不過,我想在下面的方式路由工作:

  1. 域/事件 - >列出了所有事件,有點像域/事件的默認操作
  2. 域/事件/ 3 - >顯示特定事件(ID爲3),就像domain/details/3在默認情況下一樣。
  3. domain/event/cool-event - >根據它的'slug'顯示一個特定事件,這是EventModel的一個屬性,它是Event /事件/編輯/ 3 - >編輯事件的屬性。

我一直在玩路由器,我不能讓它像我想要的那樣表現。上述邏輯是否容易實現?

+1

你檢查[屬性路由(http://blogs.msdn.com/b/webdev/archive/2013/10/17 /attribute-routing-in-asp-net-mvc-5.aspx)呢? –

+0

它應該很容易,是的 - 這是一個標準方案。所有路線都很容易區分(2/3根據數字檢查)。 – McGarnagle

回答

1

使用Attribute Routing它可能是這樣的(未經測試):

public class EventController : Controller 
{ 
    //1. domain/events -> lists all events, sort of like domain/event does by default 
    [Route("events")] 
    public ActionResult Index() 
    { 
     //TODO: Add Action Code 
     return View(); 
    } 

    //2. domain/event/3 -> show a specific event (ID of 3), just like domain/details/3 does by default. 
    [Route("event/id")] 
    public ActionResult Details(int id) 
    { 
     //TODO: Add Action Code 
     return View(); 
    } 

    //3. domain/event/cool-event -> show a specific event based on it's 'slug', which is a property of the EventModel 
    [Route("event/{slug?}")] 
    public ActionResult ViewEvent(string slug) 
    { 
     //TODO: Add Action Code 
     return View(); 
    } 

    //4. domain/event/edit/3 -> edits the event. 
    [Route("event/edit/id")] 
    public ActionResult Edit(int id) 
    { 
     //TODO: Add Action Code 
     return View(); 
    } 
} 
+1

ooo屬性路由很有光澤! – William