Now the issue was if we type the string with this URL http://localhost:61545/Kamran with out /Home/Index we get this error window.
We already saw the default route in our previous post and that's what the only route we have if we type different URL our website cannot recognise it because there is no route defined at Global.asax file. Now we are going to define new two routes for this example which are here at Global.asax file.
routes.MapRoute(
"First", // Route name
"{name}", // URL with parameters
new { controller = "Home", action = "Index", name = "" } // Parameter defaults
);
routes.MapRoute(
"Second", // Route name
"{controller}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = "" } // Parameter defaults
);
These both routes should be defined before Default route which was:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = "" } // Parameter defaults
);
Now the Global.asax file will look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace URLExample
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"First", // Route name
"{name}", // URL with parameters
new { controller = "Home", action = "Index", name = "" } // Parameter defaults
);
routes.MapRoute(
"Second", // Route name
"{controller}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
Now try another Route which is defined in "Second" : http://localhost:61545/Home/Kamran the result will be same as we already defined the Route for this URL.
Thanks to all of you who follow my post and thanks to all your support please don't forget to comment and will come back with URL Constraints.

