How RouteBase.GetRouteData or pointers work to implement pattern matching

I am looking for an implementation of an option to define specific URL patterns that my HttpModule should ignore.

I want to be able to define "filters", for example:

/Admin/{*} /Products/{*}/Search 

Which should filter out URLs, for example:

 http://mysite.com/admin/options http://mysite.com/products/toys/search 

but don't filter out http://mysite.com/orders http://mysite.com/products/view/1

Same as ASP.NET MVC, registered routes that match the pattern. I looked at the source code of debugging debugging Phil Hack, thinking that this might show me how RouteBase.GetRouteData (..) works, however it just uses it.

I can not find examples showing how this works RouteBase.GetRouteData (or find the source code for it).

If someone could point me in the right direction, as is usually done (or pattern matching), that would be great.

PS: I already know that I can use regular expressions, but I would like to have a very specific set of rules.

+1
source share
1 answer

Update

Since you want to write an HttpModule that imitates System.Web.Routing very accurately, maybe you should use ILSpy and flip the assembly and see what it does?

Original answer (saved for posterity)

It is unclear if you are talking about ASP.NET MVC or Spring MVC or Spring.NET extensions to ASP.NET MVC. If this is the first or third:

For your first example:

Admin/{*}

Decision No. 1 below will consider it. For your second example:

Products/{*}/Search , solution No. 2 will contact him (if there is a need to check or really have something valid there)

Two solutions:

  • Create routes that accept these parameters (but don't do anything with them or care about them)
  • Create an ActionFilter that validates the request for these parameters (from the route), and then replaces them with what should actually be there.

Solution 1

In the section of your routes in global.asax.cs :

 routes.MapRoute("AdminAnything", "Admin/{*anything}", new { controller = admin, action = "Show" } ); 

This will cause the following URLs to be allowed for the "Show" action in the administrator controller (thus, ignoring the entries as you wish):

 Admin/Options Admin/Anything-I-Want-here 

Decision 2

Now the second is more complicated because you have something in between.

Input Example:

 Product/{*}/Search 

You can write an ActionFilter that intercepts the request, looks at it, and replaces that value with whatever you want.

First, the desired route:

 routes.MapRoute("ProductSearch", "Products/{searchType}/Search, new { controller = "Products", action = "Search" } ); 

Then the action of your controller:

 public ActionResult Search(string searchType) { //do nothing with searchType here. } 

If you want to replace something, you can send the hidden form field in your view and handle this in the Actionfilter:

 public class SearchValidationActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if(filterContext.ActionParameters.ContainsKey("searchType") && filterContext.HttpContext.Request.Headers["hiddenSearchType"].IsNotNullOrEmpty())) { var actualSearchType = filterContext.ActionParameters["hiddenSearchType"] as SearchType; var searchType = filterContext.ActionParameters["searchType"]; if (actualSearchType != null) { if (searchType.IsNullOrEmpty()) { filterContext.Result = new SearchRedirectResult(actualSearchType.Name); } else if (!actualSearchType.Name.Equals(searchType)) { filterContext.Result = new SearchRedirectResult(actualSearchType.Name,); } } } base.OnActionExecuting(filterContext); } } 

Basically, Decision No. 2 takes whatever you want and, based on the type of hidden search, passes this to the actual type of search.

+1
source

All Articles