2015-11-19 2 views
8

dies ist mein Codeich möchte eine ID nach localhost Port-Nummer mit Regeln in web.config hinzufügen

<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> 

das ist mein 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; 
    } 
} 

aber wenn ich laufen diese ich bin immer die uRL-Pfad als 'http://localhost:49363/' aber ich will 'http://localhost:49363/12312312'

+0

Warum verwenden Sie 'RouteConfig' nicht? – Azim

+0

Wie kann ich das mit RouteConfig machen ??? –

Antwort

7

Nach etwas mehr R & D auf diese Weise Schließlich bekam ich die Lösung

<rewrite> 
    <rules> 
<rule name="AppId" stopProcessing="true"> 
    <match url="^$" /> 
    <action type="Redirect" url="/12312312" redirectType="Permanent" /> 
</rule> 
    </rules> 
</rewrite> 
1

ändern Sie Ihre Standardroute:

routes.MapRoute(
      name: "Default", 
      url: "12312312/{controller}/{action}/{id}", 
      defaults: new { action = "Index", id = UrlParameter.Optional } 
      ); 

Update:

versuchen, eine zusätzliche Route, bevor die aktuelle Route für Ihre spezifische Aktion Hinzufügen

routes.MapRoute(
      name: "MyRoute", 
      url: "12312312", 
      defaults: new { controller = "YourController", action = "YourAction" } 
      ); 
+0

Aber wenn ich dies zu meinem route.config hinzufügen, werden meine anderen Routen –

+0

auswirken, auch wenn ich dies hinzufügen, funktioniert es nicht –

+0

Versuchen Sie meine aktualisierte Antwort. Hoffe das hilft. – Azim

Verwandte Themen