mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Is it possible to define a route to "Icon/{*pathinfo}" #213

Closed IanMercer closed 11 years ago

IanMercer commented 11 years ago

Would be nice if you could define a route like that and get an IEnumerable<string> pathInfo as the parameter.

mccalltd commented 11 years ago

That would be pretty cool. Is this something that should be independent of AR though, as a general routing util?

Bitsonthefloor commented 11 years ago

Actually all you would need is a model binder that splits a string by '/' or whatever character your want and then set your action parameter as

public ActionResult Icon([ModelBinder(typeof(SplitStringBinder))] IEnumerable<string> pathInfo) {
        class StringStringModeBinder : IModelBinder
        {

            #region IModelBinder Members

            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
                {
                    var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                    if (value != null)
                        return value.AttemptedValue.Split('/');
                }
                return null;
            }

            #endregion
        }

This is rough and untested but it should be in the right direction.

IanMercer commented 11 years ago

I came across this whilst migrating an existing application to AttributeRouting. There was an instance of {*pathinfo} in an existing route that I needed to migrate. It would be nice to have it in AttributeRouting to make migration easy. Admittedly the use in this case was more of an "abuse" (passing a list of items) so maybe I should just fix that!

mccalltd commented 11 years ago

Hi Ian, going to close this as I feel it's outside the scope of AR. Good luck with your implementation!