我想使用web.config中的规则在localhost portnumber之后添加一个id

时间:2015-11-19 06:31:45

标签: asp.net-mvc asp.net-mvc-4

这是我的代码

<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

2 个答案:

答案 0 :(得分:7)

在此之后再做一些R&amp; D,最终得到了解决方案

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

答案 1 :(得分:1)

将您的默认路线更改为:

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

<强>更新

尝试在您的特定操作的当前路线之前添加额外路线

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