chuckd
chuckd

Reputation: 14520

How do I construct my action method for a specific route Asp.Net MVC

I have two routes I'm trying to create to use like

www.mysite.com/Rate/Student/Event/123

www.mysite.com/Rate/Teacher/Event/1234

routes.MapRoute(
    name: "Rate",
    url: "Rate/Student/Event/{id}"
);

routes.MapRoute(
    name: "Rate",
    url: "Rate/Teacher/Event/{id}"
);

How do I construct the action methods?

Here is what I have in my Rate controller

public ActionResult Student(int id)
{
    return View();
}

public ActionResult Teacher(int id)
{
    return View();
}

Upvotes: 1

Views: 688

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

You have set up routing to match the URL, but you haven't told MVC where to send the request. MapRoute works by using route values, which can either be defaulted to specific values or passed through the URL. But, you are doing neither.

NOTE: The controller and action route values are required in MVC.

Option 1: add default route values.

    routes.MapRoute(
        name: "Rate",
        url: "Rate/Student/Event/{id}",
        defaults: new { controller = "Rate", action = "Student" }
    );

    routes.MapRoute(
        name: "Rate",
        url: "Rate/Teacher/Event/{id}",
        defaults: new { controller = "Rate", action = "Teacher" }
    );

Option 2: pass the route values through the URL.

    routes.MapRoute(
        name: "Rate",
        url: "{controller}/{action}/Event/{id}"
    );

Upvotes: 4

Related Questions