Model view controller 如何在运行时删除路由?
我正在使用名为AttributeRouting的第三方nuget,它使用属性注册路由。我有一个独特的情况,我需要在应用程序启动或类似操作时从路由表中删除路由。如何做到这一点 我已经提供了我想要删除的路线的屏幕截图。我甚至把它命名为“RemoveMePlease”。 谢谢Model view controller 如何在运行时删除路由?,model-view-controller,asp.net-mvc-3,asp.net-mvc-routing,Model View Controller,Asp.net Mvc 3,Asp.net Mvc Routing,我正在使用名为AttributeRouting的第三方nuget,它使用属性注册路由。我有一个独特的情况,我需要在应用程序启动或类似操作时从路由表中删除路由。如何做到这一点 我已经提供了我想要删除的路线的屏幕截图。我甚至把它命名为“RemoveMePlease”。 谢谢公共类MVCAPApplication:admin.project.Global { 受保护的覆盖无效应用程序\u Start() { base.Application_Start(); int iRoute=0; while(
公共类MVCAPApplication:admin.project.Global
{
受保护的覆盖无效应用程序\u Start()
{
base.Application_Start();
int iRoute=0;
while(iRoute=(iRoute+1);iDup--)
{
if(RouteTable.Routes[iDup]是AttributeRoute)
{
var potentialDupRoute=RouteTable.Routes[iDup]作为AttributeRoute;
如果(currentRoute.Url.Equals(potentialDupRoute.Url))/--路由在Url上匹配
{
//--httpMethods也匹配吗?
//--属性运算3.1
ICollection currentHttpMethods=(currentRoute.Constraints[“inboundHttpMethod”]作为AttributeRouting.Web.Mvc.Constraints.InboundHttpMethodConstraint)。AllowedMethods;
ICollection potentialHttpMethods=(potentialDupRoute.Constraints[“inboundHttpMethod”]作为AttributeRouting.Web.Mvc.Constraints.InboundHttpMethodConstraint)。允许的方法;
IEnumerable matchedHttpMethods=currentHttpMethods.Intersect(潜在HttpMethods);
//--移除路线
if(matchedHttpMethods.Count()==currentHttpMethods.Count())RouteTable.Routes.Remove(currentRoute);
}
}
}
}
iRoute++;
}
}
}
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++;
}
}
}