当前位置: 首页>>代码示例>>C#>>正文


C# RouteCollection.MapDelegate方法代码示例

本文整理汇总了C#中RouteCollection.MapDelegate方法的典型用法代码示例。如果您正苦于以下问题:C# RouteCollection.MapDelegate方法的具体用法?C# RouteCollection.MapDelegate怎么用?C# RouteCollection.MapDelegate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RouteCollection的用法示例。


在下文中一共展示了RouteCollection.MapDelegate方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RegisterRoutes

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // Redirect From Old Route to New route
            var targetRoute = routes.Map("target", "yo/{id}/{action}", new { controller = "Home" });
            routes.Redirect(r => r.MapRoute("legacy", "foo/{id}/baz/{action}")).To(targetRoute, new { id = "123", action = "index" });
            routes.Redirect(r => r.MapRoute("legacy2", "foo/baz")).To(targetRoute, new { id = "123", action = "index" });

            // Map Delegate
            routes.MapDelegate("map-delegate", "this-is-a-test", rc => rc.HttpContext.Response.Write("Yeah, it's a test"));
            routes.MapDelegate("map-delegate-incoming-only", "this-is-a-test", new { whatever = new IncomingOnlyRouteConstraint() }, rc => rc.HttpContext.Response.Write("Yeah, it's a test"));

            // Map HTTP Handlers
            routes.MapHttpHandler<HelloWorldHttpHandler>("hello-world", "handlers/helloworld");
            routes.MapHttpHandler("hello-world2", "handlers/helloworld2", new HelloWorldHttpHandler());

            RouteCollection someRoutes = new RouteCollection();
            someRoutes.MapHttpHandler<HelloWorldHttpHandler>("hello-world3", "handlers/helloworld3");
            someRoutes.MapHttpHandler("hello-world4", "handlers/helloworld4", new HelloWorldHttpHandler());
            var groupRoute = new GroupRoute("~/section", someRoutes);
            routes.Add("group", groupRoute);

            var mvcRoutes = new RouteCollection();
            mvcRoutes.Map("foo1", "foo/{controller}", new { action = "index" });
            mvcRoutes.Map("foo2", "foo2/{controller}", new { action = "index" });
            routes.Add("group2", new GroupRoute("~/group2sec", mvcRoutes));

            var defaultRoute = routes.Map(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            ).SetRouteName("Default");
        }
开发者ID:intertrendSoftware,项目名称:RouteMagic-ITFork,代码行数:33,代码来源:Global.asax.cs

示例2: MapRoutes

        private static void MapRoutes(RouteCollection routes)
        {
            var routeBase = routes["CreatePackage"];
            routes.Remove(routeBase);
            routes.Remove(routes["CreatePackage-Root"]);

            routes.MapDelegate("Override_CreatePackage",
                "api/v2/package",
                new { httpMethod = new HttpMethodConstraint("PUT") },
                context => CreateSymbolService().CreatePackage(context.HttpContext));

            routes.MapDelegate("Override_CreatePackage-Root",
                "",
                new { httpMethod = new HttpMethodConstraint("PUT") },
                context => CreateSymbolService().CreatePackage(context.HttpContext));

            routes.MapDelegate("GetSymbols",
                "symbols/{*path}",
                new {httpMethod = new HttpMethodConstraint("GET")},
                context =>CreateSymbolService().GetSymbols(context));

            routes.MapDelegate("GetSource",
                "source/{id}/{version}/{*path}",
                new {httpMethod = new HttpMethodConstraint("GET")},
                context => CreateSymbolService().GetSource(context));
        }
开发者ID:fhchina,项目名称:nupeek,代码行数:26,代码来源:NuPeek.cs

示例3: MapRoutes

        private static void MapRoutes(RouteCollection routes)
        {
            // Route to create a new package
            routes.MapDelegate("CreatePackage",
                               "api/v2/package",
                               new { httpMethod = new HttpMethodConstraint("PUT") },
                               context => CreatePackageService().CreatePackage(context.HttpContext));

            // Route to delete packages
            routes.MapDelegate("DeletePackage",
                               "api/v2/package/{packageId}/{version}",
                               new { httpMethod = new HttpMethodConstraint("DELETE") },
                               context => CreatePackageService().DeletePackage(context.HttpContext));

            // Route to get packages
            routes.MapDelegate("DownloadPackage",
                               "api/v2/package/{packageId}/{version}",
                               new { httpMethod = new HttpMethodConstraint("GET") },
                               context => CreatePackageService().DownloadPackage(context.HttpContext));

#if DEBUG
            // The default route is http://{root}/nuget/Packages
            var factory = new DataServiceHostFactory();
            var serviceRoute = new ServiceRoute("nuget", factory, typeof(Packages));
            serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" } };
            serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };
            routes.Add("nuget", serviceRoute);
#endif
        }
开发者ID:monoman,项目名称:NugetCracker,代码行数:29,代码来源:Routes.cs

示例4: MapRoutes

        private static void MapRoutes(RouteCollection routes) {
            // Route to create a new package(http://{root}/nuget)
            routes.MapDelegate("CreatePackageNuGet",
                               "nuget",
                               new { httpMethod = new HttpMethodConstraint("PUT") },
                               context => CreatePackageService().CreatePackage(context.HttpContext));

            // The default route is http://{root}/nuget/Packages
            var factory = new DataServiceHostFactory();
            var serviceRoute = new ServiceRoute("nuget", factory, typeof(Packages));
            serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" } };
            serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };
            routes.Add("nuget", serviceRoute);
        }
开发者ID:finsaspa,项目名称:Caravan,代码行数:14,代码来源:Routes.cs

示例5: MapDelegateWithParametersInUrlMatchesForUrlGeneration

        public void MapDelegateWithParametersInUrlMatchesForUrlGeneration()
        {
            // Arrange
            var routes = new RouteCollection();
            var httpRequest = new HttpRequest("foo", "http://foo.com/", "");
            var httpResponse = new HttpResponse(new Mock<TextWriter>().Object);
            var httpContext = new HttpContext(httpRequest, httpResponse);
            var requestContext = new RequestContext(new HttpContextWrapper(httpContext), new RouteData());

            // Act
            routes.MapDelegate("route-name", "{url}", c => { });
            var vp = routes[0].GetVirtualPath(requestContext, new RouteValueDictionary { { "url", "blah" } });

            // Assert
            PAssert.IsTrue(() => vp != null);
        }
开发者ID:rdkhatch,项目名称:RouteMagic,代码行数:16,代码来源:RouteExtensionsTest.cs

示例6: MapRoutes

        public static void MapRoutes(RouteCollection routes)
        {
            Action<RequestContext> processPage = context =>
            {
                var request = context.HttpContext.Request;
                var response = context.HttpContext.Response;

                var pageUrl = request.Url.AbsolutePath.TrimEnd('/');
                if (String.IsNullOrEmpty(pageUrl))
                {
                    pageUrl = "/";
                }

                // check if page was permanently redirected
                var redirectUrl = Site.GetRedirect(pageUrl);
                if (redirectUrl != null)
                {
                    response.RedirectPermanent(redirectUrl, true);
                }
                else
                {
                    var page = Site.GetPage(pageUrl);
                    if (page == null || !page.Published)
                    {
                        // 404
                        response.StatusCode = 404;
                        response.StatusDescription = "Page not found";
                        response.End();
                    }
                    else
                    {
                        // render page
                        response.ContentType = "text/html";
                        response.Write(Site.RenderPage(page, request.Url.PathAndQuery));
                        response.End();
                    }
                }
            };

            // Routes
            routes.MapDelegate("Default", "{*catchall}", new { httpMethod = new HttpMethodConstraint("GET") }, processPage);
        }
开发者ID:ili,项目名称:website,代码行数:42,代码来源:Routes.cs

示例7: MapDelegateSetsRouteNameAndHttpHandlerRouteHandler

        public void MapDelegateSetsRouteNameAndHttpHandlerRouteHandler()
        {
            // Arrange
            var httpHandler = new Mock<IHttpHandler>().Object;
            var routes = new RouteCollection();
            bool isSet = false;
            var httpRequest = new HttpRequest("foo", "http://foo.com/", "");
            var httpResponse = new HttpResponse(new Mock<TextWriter>().Object);
            var httpContext = new HttpContext(httpRequest, httpResponse);
            var requestContext = new RequestContext(new HttpContextWrapper(httpContext), new RouteData());

            // Act
            var route = routes.MapDelegate("route-name", "url", c => isSet = true);
            route.RouteHandler.GetHttpHandler(requestContext).ProcessRequest(httpContext);

            // Assert
            PAssert.IsTrue(() => route.GetRouteName() == "route-name");
            PAssert.IsTrue(() => route.RouteHandler.GetHttpHandler(requestContext).GetType() == typeof(DelegateHttpHandler));
            PAssert.IsTrue(() => isSet == true);
        }
开发者ID:rdkhatch,项目名称:RouteMagic,代码行数:20,代码来源:RouteExtensionsTest.cs

示例8: MapRoutes

        public static void MapRoutes(RouteCollection routes)
        {
            Action<RequestContext> processPage = context =>
            {
                var request = context.HttpContext.Request;
                var response = context.HttpContext.Response;

                //var originalUrl = request.Url;
                //if(request.HttpMethod == "GET" && originalUrl.Host != "localhost" && (originalUrl.Scheme != "https" || !originalUrl.Host.StartsWith("www.")))
                //{
                //    // redirect to https://www.appveyor.com
                //    var httpsUrl = new UriBuilder(originalUrl);
                //    httpsUrl.Scheme = "https";
                //    httpsUrl.Port = -1;
                //    if(!httpsUrl.Host.StartsWith("www."))
                //    {
                //        httpsUrl.Host = "www." + httpsUrl.Host;
                //    }
                //    response.RedirectPermanent(httpsUrl.ToString(), true);
                //    return;
                //}

                var originalUrl = request.Url;
                if (request.HttpMethod == "GET" && originalUrl.Host != "localhost" && !originalUrl.Host.StartsWith("www."))
                {
                    // redirect to www.appveyor.com
                    var wwwUrl = new UriBuilder(originalUrl);
                    wwwUrl.Host = "www." + wwwUrl.Host;

                    response.RedirectPermanent(wwwUrl.ToString(), true);
                    return;
                }

                var pageUrl = request.Url.AbsolutePath.TrimEnd('/');
                if (String.IsNullOrEmpty(pageUrl))
                {
                    pageUrl = "/";
                }

                // check if page was permanently redirected
                var redirectUrl = Site.GetRedirect(pageUrl);
                if (redirectUrl != null)
                {
                    response.RedirectPermanent(redirectUrl, true);
                }
                else
                {
                    var page = Site.GetPage(pageUrl);
                    if (page == null || !page.Published)
                    {
                        // 404
                        response.StatusCode = 404;
                        response.StatusDescription = "Page not found";
                        response.End();
                    }
                    else
                    {
                        // render page
                        response.ContentType = "text/html";
                        response.Write(Site.RenderPage(page, request.Url.PathAndQuery));
                        response.End();
                    }
                }
            };

            // Routes
            routes.MapDelegate("Default", "{*catchall}", new { httpMethod = new HttpMethodConstraint("GET") }, processPage);
        }
开发者ID:mteper,项目名称:website,代码行数:68,代码来源:Routes.cs

示例9: RegisterRoutes

        public static void RegisterRoutes( RouteCollection routes )
        {
            routes.IgnoreRoute( "favicon.ico" );
            routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );

            //routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);

            //routes.MapRoute(
            //    name: "Default",
            //    url: "{action}",
            //    defaults: new { lang = "fr", controller = "Home", action = "Index" }
            //);

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

            const string defautlRouteUrl = "{action}";
            RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary( new { controller = "Home", action = "Index" } );

            //Route defaultRoute = new Route( defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler() );

            //routes.Add( "HomeLocalized", new LocalizedRoute( "", defaultRouteValueDictionary ) );
            routes.Add( "DefaultLocalized", new LocalizedRoute( defautlRouteUrl, defaultRouteValueDictionary ) );
            routes.MapDelegate( "Default", defautlRouteUrl, ( rq ) =>
                {
                    var userLanguages = rq.HttpContext.Request.UserLanguages;
                    if( userLanguages != null && userLanguages.Length > 0 )
                    {
                        try
                        {
                            var ci = new CultureInfo( userLanguages[0] );
                            if( ci != null )
                            {
                                rq.RouteData.Values[LocalizedRoute.CultureKey] = ci.TwoLetterISOLanguageName;
                            }
                            else
                            {
                                rq.RouteData.Values[LocalizedRoute.CultureKey] = CultureManager.DefaultCulture.TwoLetterISOLanguageName;
                            }
                        }
                        catch( CultureNotFoundException )
                        {
                            rq.RouteData.Values[LocalizedRoute.CultureKey] = CultureManager.DefaultCulture.TwoLetterISOLanguageName;
                        }
                    }
                    else
                    {
                        rq.RouteData.Values[LocalizedRoute.CultureKey] = CultureManager.DefaultCulture.TwoLetterISOLanguageName;
                    }
                    rq.HttpContext.Response.Redirect( RouteTable.Routes.GetVirtualPath( rq, null ).VirtualPath, false );
                } ).Defaults = defaultRouteValueDictionary;

            //routes.Add( "Default", new Route( defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler() ) );
        }
开发者ID:Invenietis,项目名称:invenietis-website,代码行数:61,代码来源:RouteConfig.cs


注:本文中的RouteCollection.MapDelegate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。