I have been following
this tutorial, hoping to understand ASP.NET better (I've worked with it for years, but never learned how it is set up).
I am having trouble understanding how routing works. In the following code I have set up three routings to variations of the pages created in the tutorials.
In each routing, there are defaults set for the
controller,
action and
id, but only the first routing lets those values default. The
ViewData and
ViewBag routes will not accept anything other than a fully filled in URL. Why not?
Also, in each, the
id is marked as optional, but again, only the first routing will let
id be omitted. The other two won't accept
/Index as the end of the URL. Why not?
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace FirstMVCDemo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ViewData",
url: "ViewData/{controller}/{action}/{id}",
defaults: new { controller = "HomeVD", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ViewBag",
url: "ViewBag/{controller}/{action}/{id}",
defaults: new { controller = "HomeVB", action = "Index", id = UrlParameter.Optional }
);
}
}
}