さらにグーグルで調べた後、この投稿の質問を言い換えました。その質問からの回答を使用して、以下に含める適切なルーティング拡張方法をまとめることができました。
public static class RouteCollectionExtensions
{
public static void CustomMapRoute( this RouteCollection routes, string name, string url, object defaults )
{
routes.CustomMapRoute( name, url, defaults, null, null );
}
public static void CustomMapRoute( this RouteCollection routes, string name, string url, object defaults, string[] namespaces )
{
routes.CustomMapRoute( name, url, defaults, namespaces, null );
}
public static void CustomMapRoute( this RouteCollection routes, string name, string url, object defaults, string[] namespaces, object constraints )
{
if ( routes == null )
throw new ArgumentNullException( "routes" );
if ( url == null )
throw new ArgumentNullException( "url" );
var route = new CustomRoute( url, new MvcRouteHandler() )
{
Defaults = new RouteValueDictionary( defaults ),
Constraints = new RouteValueDictionary( constraints ),
DataTokens = new RouteValueDictionary()
};
if ( (namespaces != null) && (namespaces.Length > 0) )
{
route.DataTokens["Namespaces"] = namespaces;
}
if ( String.IsNullOrEmpty( name ) )
routes.Add( route );
else
routes.Add( name, route );
}
}
public class CustomRoute : Route
{
public CustomRoute( string url, IRouteHandler routeHandler )
: base( url, routeHandler ) { }
public CustomRoute( string url, RouteValueDictionary defaults, IRouteHandler routeHandler )
: base( url, defaults, routeHandler )
{ }
public override VirtualPathData GetVirtualPath( RequestContext requestContext, RouteValueDictionary values )
{
var path = base.GetVirtualPath( requestContext, values );
if ( path != null )
{
path.VirtualPath = path.VirtualPath.Replace( "%20", "+" );
}
return path;
}
}
この拡張機能は、global.asax の RegisterRoutes メソッドによって次のように呼び出されます...
routes.CustomMapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyControllers" }
);