这是我的代码
<rule name="adding Id after PortNumber" patternSyntax="Wildcard" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="{HTTP_HOST}/12312312" negate="true"/> </conditions> <action type="Redirect" url="{HTTP_HOST}/{R:1}"/> </rule>
这是我的route.config
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "ApplicationRoute","{appId}/{controller}/{action}/{id}",new { controller = "Account",action = "SignIn",id = UrlParameter.Optional },new { isValidAppId = new isValidAppId() } ); } } public class isValidAppId : IRouteConstraint { public bool Match(HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection) { var isValid = false; if (values["appId"] != null && WebConfigurationManager.AppSettings["ModelApplicationId"] != null) { if (values["appId"].ToString() == WebConfigurationManager.AppSettings["ModelApplicationId"].ToString()) return isValid = true; } // return true if this is a valid AppId return isValid; } }
但是,当我运行这个我得到的网址为’http://localhost:49363/‘,但我想’http://localhost:49363/12312312‘
解决方法
在做了更多R& D之后,最终得到了解决方案
<rewrite> <rules> <rule name="AppId" stopProcessing="true"> <match url="^$" /> <action type="Redirect" url="/12312312" redirectType="Permanent" /> </rule> </rules> </rewrite>