3

I'm trying to create a custom route for URL with the following format:

http://domain/nodes/{item_1}/{item_2}/{item3_}/..../{item_[n]}

Basically, there could be a random amount of item_[n], for example

http://domain/nodes/1/3/2 

http://domain/nodes/1

http://domain/nodes/1/25/11/45

With my custom route I would like to retrieve an array of items and do some logic (validate and add some specific information to request context) with them.

For example from [http://domain/nodes/1/25/11/45] I would like to get an array of [1, 25, 11, 45] and process it.

So, I have 2 problems here.

The first one is a question actually. Am I looking in the right direction? Or there could be an easier way to accomplish this (maybe without custom routes)?

The second problem is matching incoming url with a regex pattern. Could someone help me with it?

Thanks in advance :)

2 Answers 2

1

To solve your problem I think that a way could be to create a routing class and then handle the params accordinlgy.

  public class CustomRouting : RouteBase
  {
     public override RouteData GetRouteData(HttpContextBase httpContext)
     {
       RouteData result = null;
       var repository = new FakeRouteDB(); //Use you preferred DI injector
       string requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath;
       string[] sections = requestUrl.Split('/');
       /*
       from here you work on the array you just created
       you can check every single part
       */
       if (sections.Count() == 2 && sections[1] == "")
         return null; // ~/

       if (sections.Count() > 2) //2 is just an example
       {
         result = new RouteData(this, new MvcRouteHandler());
         result.Values.Add("controller", "Products");
         result.Values.Add("action", "Edit");
         result.Values.Add("itmes0", sections[1]);
         if (sections.Count() >= 3)
         result.Values.Add("item2", sections[2]);
         //....
       }
       else
       {
         //I can prepare a default route        
         result = new RouteData(this, new MvcRouteHandler());
         result.Values.Add("controller", "Home");
         result.Values.Add("action", "Index");
       }
       return result;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
     //I just work with outbound so it's ok here to do nothing
     return null;
    }
}

In the global.asax

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

  routes.Add(new CustomRouting());

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

This should give you an idea of what I think. Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

0

I can't help you with the first part of your question, but I can have a go at creating the regex.

In your example all the items are digits - is that the only option ? If not, please provide more info on possible characters.

For now the regex would be:

@"http://domain/nodes(?:/(\d+))*"

(?:) is a non capturing group, () is a capturing group.
If you match all occurences, then you'll end up with groups 1-n, where each group will contain the matched number (group number 0 will be the whole match).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.