0
我正在使用名爲AttributeRouting的第三方nuget,它使用屬性註冊路由。我有一個獨特的情況,我需要從Application_Start或類似的路由表中刪除路由。如何才能做到這一點?如何在運行時刪除路由?
我提供了我想要刪除的路線的屏幕截圖。我甚至將它命名爲「RemoveMePlease」。
感謝
我正在使用名爲AttributeRouting的第三方nuget,它使用屬性註冊路由。我有一個獨特的情況,我需要從Application_Start或類似的路由表中刪除路由。如何才能做到這一點?如何在運行時刪除路由?
我提供了我想要刪除的路線的屏幕截圖。我甚至將它命名爲「RemoveMePlease」。
感謝
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++;
}
}
}