I am using a third party nug开发者_开发技巧et called AttributeRouting which registeres routes using attributes. I have a unique situation and i need to remove a route from the route table on Application_Start or similar. How can this be done?
I have supplied a screenshot of the route i want to remove. I have even named it "RemoveMePlease".
thanks
public class MvcApplication : admin.project.Global
{
protected override void Application_Start()
{
base.Application_Start();
int iRoute = 0;
while (iRoute < RouteTable.Routes.Count)
{
bool isLastRoute = (iRoute == (RouteTable.Routes.Count - 1));
RouteBase route = RouteTable.Routes[iRoute];
if (!isLastRoute && route is AttributeRoute)
{
AttributeRoute currentRoute = route as AttributeRoute;
int maxRouteIndex = RouteTable.Routes.Count - 1;
for (int iDup = maxRouteIndex; iDup >= (iRoute + 1); iDup--)
{
if (RouteTable.Routes[iDup] is AttributeRoute)
{
var potentialDupRoute = RouteTable.Routes[iDup] as AttributeRoute;
if (currentRoute.Url.Equals(potentialDupRoute.Url)) //-- routes match on url
{
//-- do httpMethods also match?
//-- AttributeRouting <=3.1
ICollection<string> currentHttpMethods = ((currentRoute.Constraints["httpMethod"]) as HttpMethodConstraint).AllowedMethods;
ICollection<string> potentialHttpMethods = ((potentialDupRoute.Constraints["httpMethod"]) as HttpMethodConstraint).AllowedMethods;
//-- AttributeRouting > 3.1
ICollection<string> currentHttpMethods = (currentRoute.Constraints["inboundHttpMethod"] as AttributeRouting.Web.Mvc.Constraints.InboundHttpMethodConstraint).AllowedMethods;
ICollection<string> potentialHttpMethods = (potentialDupRoute.Constraints["inboundHttpMethod"] as AttributeRouting.Web.Mvc.Constraints.InboundHttpMethodConstraint).AllowedMethods;
IEnumerable<string> matchedHttpMethods = currentHttpMethods.Intersect(potentialHttpMethods);
//-- remove the route
if (matchedHttpMethods.Count() == currentHttpMethods.Count()) RouteTable.Routes.Remove(currentRoute);
}
}
}
}
iRoute++;
}
}
}
精彩评论