我已经开始做ASP.NET MVC,但我不知道从哪个问题开始。
我创建了一个默认应用程序,并创建了一个EventModel,EventController和一系列默认的Event视图。一切正常。
但是,我想以下列方式使路由工作:
我一直在玩路由器,我不能让它表现得像我想要的那样。以上逻辑是否易于实现?
答案 0 :(得分:1)
使用Attribute Routing可能就像这样(UNTESTED):
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();
}
}