0

I have started doing ASP.NET MVC, but I don't know where to start with this question.

I have created a default application and I have created an EventModel, EventController and a series of the default Event views. It is all working correctly.

However, I want to make the routing work in the following way:

  1. domain/events -> lists all events, sort of like domain/event does by default
  2. domain/event/3 -> show a specific event (ID of 3), just like domain/details/3 does by default.
  3. domain/event/cool-event -> show a specific event based on it's 'slug', which is a property of the EventModel
  4. domain/event/edit/3 -> edits the event.

I have been playing around with the router and I can't get it to behave like I want it to. Is the above logic easy to implement?

2
  • 1
    have you checked attribute routing yet? Commented Dec 17, 2013 at 20:36
  • It should be easy, yes -- that's a standard scenario. All routes are easily distinguishable (2/3 based on the numeric check). Commented Dec 17, 2013 at 20:46

1 Answer 1

1

Using Attribute Routing it could be like this (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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

ooo attribute routing is shiny!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.