This sort of thing is pretty easy to do by subclassing RouteBase
. It allows you to use virtually any logic you want to analyze the request and direct it to specific action methods depending on whats in the request.
Here is a route using your example of http://localhost/Default.aspx?action=Index2 and http://localhost/Default.aspx?action=Edit
public class CustomRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
// Trim off the leading "/"
var path = httpContext.Request.Path.Substring(1);
if (path.Equals("Default.aspx", StringComparison.OrdinalIgnoreCase))
{
result = new RouteData(this, new MvcRouteHandler());
result.Values["controller"] = "Home";
result.Values["action"] = httpContext.Request.QueryString["action"] ?? "Index";
}
// IMPORTANT: Returning null tells the routing framework to try
// the next registered route.
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
// Returning null skips this route.
// Alternatively, implement a scheme to turn the values passed here
// into a URL. This will be used by UrlHelper to build URLs in views.
return null;
}
}
It can be registered with MVC like this.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new CustomRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WebApplication23.Controllers" }
);
}
}
Do note that the order routes are registered is important.
There is one more thing if you insist on using a filename.extension
pattern - you have to configure IIS to allow the request to reach MVC because by default any URL with a .
in it will throw a 404 not found error.
<system.webServer>
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0"
path="/Default.aspx"
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>