使用自定义routebase类时,不会调用Controller Action Method

时间:2016-05-13 07:30:01

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

以下是Custom RouteBase类

 public class CustomRoutingBehaviour:RouteBase
        {
            string[] allowedUrl;
            public CustomRoutingBehaviour(string[] url)
            {
                allowedUrl = url;
            }

            public override RouteData GetRouteData(HttpContextBase httpContext)
            {
                RouteData route = null;
                string browsedurl = httpContext.Request.AppRelativeCurrentExecutionFilePath;
                if(allowedUrl.Contains(browsedurl,StringComparer.OrdinalIgnoreCase))
                {
                    route = new RouteData(this,new MvcRouteHandler());
                    route.Values.Add("controller","CustomiziedController");
                    route.Values.Add("action","Index");
                    route.Values.Add("BrowsedURL",browsedurl);

                }

            return route;
            }

            public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
            {
                return null;
            }
        }

以下是路由配置:

routes.Add(new CustomRoutingBehaviour(new string[] { "~/Customizied/Index", "~/Customizied/Index1" }));

1 个答案:

答案 0 :(得分:0)

我有完全相同的问题。我已经解决了在浏览器的url地址添加结尾斜杠( / )的问题。注意处理代码中的斜杠。您可以在 CustomRoutingBehaviour 类或子串 browsedurl 变量的ctor中传递的数组的每个网址上添加最终斜杠。 我不知道为什么会这样。