本文整理汇总了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");
}
示例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));
}
示例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
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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() ) );
}