Sembra un percorso personalizzato potrebbe tagliare la senape:
public class MyRoute: Route
{
public MyRoute()
: base("{*catchall}", new MvcRouteHandler())
{
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var rd = base.GetRouteData(httpContext);
if (rd == null)
{
// we do not have a match for {*catchall}, although this is very
// unlikely to ever happen :-)
return null;
}
var segments = httpContext.Request.Url.AbsolutePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length < 4)
{
// we do not have the minimum number of segments
// in the url to have a match
return null;
}
if (!string.Equals("items", segments[1], StringComparison.InvariantCultureIgnoreCase) &&
!string.Equals("items", segments[2], StringComparison.InvariantCultureIgnoreCase))
{
// we couldn't find "items" at the expected position in the url
return null;
}
// at this stage we know that we have a match and can start processing
// Feel free to find a faster and more readable split here
string repository = string.Join("/", segments.TakeWhile(segment => !string.Equals("items", segment, StringComparison.InvariantCultureIgnoreCase)));
string path = string.Join("/", segments.Reverse().TakeWhile(segment => !string.Equals("items", segment, StringComparison.InvariantCultureIgnoreCase)).Reverse());
rd.Values["controller"] = "items";
rd.Values["action"] = "index";
rd.Values["repository"] = repository;
rd.Values["path"] = path;
return rd;
}
}
che potrebbe essere registrato prima che i percorsi standard:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("myRoute", new MyRoute());
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
E se avete intenzione di mettere stringhe arbitrarie nella parte del percorso dei tuoi URL, spero tu sia a conoscenza dello Zombie Operating Systems
che potrebbe sorprenderti.
fonte
2014-07-24 15:56:45
Deve essere quell'URL esatto, o quei parametri possono essere codificati tramite URL? (Sto indovinando il primo, dal momento che quest'ultimo è un * molto * più facile, ma vale la pena chiedere esplicitamente.) – David
@David: Vedi le mie modifiche –
Se non puoi codificare l'URL, l'unica cosa che puoi fare è prendere il l'intero URL come stringa e analizzalo tu stesso. – DavidG