user1932703
user1932703

Reputation: 13

Create a route with an additional url parameter without creating an area

I'm building a site for a client using .Net MVC 4. The entire site uses the default MVC 4 route and all the pages work fine

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        )

There is a page on the site called 'Teachers'. For this page, there are several links that take you to new pages that are subsets of the 'Teachers' page. The client wants the url structure to appear like this

www.{mysite}.com/School/Teachers/Apply

www.{mysite}.com/School/Teachers/Benefits

I thought I could simple add the Apply and Benefits pages as an ActionResult in my SchoolController then use the routing feature in MVC to map the url to the correct ActionResult method in the SchoolController.

This is my controller:

public class SchoolController : Controller
{

    public ActionResult Index()
    {

        return View();
    }

    public ActionResult Administration()
    {
        return View();
    }

    public ActionResult Teachers()
    {
        return View();
    }

    public ActionResult Apply()
    {
        return View();
    }

    public ActionResult Benefits()
    {
        return View();
    }
}

This is the custom route that I tried.

routes.MapRoute(
       "Teachers",
       "{controller}/{page}/{action}",
       new { controller = "School", page = "Teachers", action = "Index" }
           )

I placed this route before the default route but this adds 'teachers' to every url on the site like this:

www.{mysite}.com/{controller}/teachers/{action}

SUMMARY

All the pages on my site use this url structure:

www.{mysite}.com/{controller}/{action}

This one page, however, has the following structure:

www.{mysite}.com/{controller}/teachers/{action}

How can I do this with routes?

Upvotes: 1

Views: 1144

Answers (1)

asymptoticFault
asymptoticFault

Reputation: 4529

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "TeachersActions",
        url: "School/Teachers/{action}",
        defaults: new { controller = "School" },
        constraints: new { action = "Apply|Benefits" } // actions under Teachers
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index",
                        id = UrlParameter.Optional }
    );
}

Any actions you want to be under Teachers should be added to the route. It is actually not necessary to specify defaults for action or page for this route to work (unless you do have some need for a page route value to be captured). The catch here is that a user can still target the actions under Teachers by entering the URL School/{action} because it is caught by the default route. Now this may or may not be a concern for you. Personally I would not consider it such a big issue since the users should just be using the site's navigation instead of entering the URLs manually.

Upvotes: 1

Related Questions