Ok, this seems to work!

    public class PatternRouteWithParameters : PatternRoute
    {
        private readonly Dictionary<string, string> parameters;

        public PatternRouteWithParameters(string pattern, Dictionary<string,
string> parameters)
            : base(pattern)
        {
            this.parameters = parameters;
        }

        public PatternRouteWithParameters(string name, string pattern,
Dictionary<string, string> parameters)
            : base(name, pattern)
        {
            this.parameters = parameters;
        }

        public override int Matches(string url, IRouteContext context,
RouteMatch match)
        {
            var matchResult = base.Matches(url, context, match);
            if (parameters != null)
                foreach (var key in parameters.Keys)
                    match.Parameters.Add(key, parameters[key]);
            return matchResult;
        }
    }

But unfortunately only one way - I have not found any way to have
${Url.For()} prefer, without the help of the named parameter, the specific
url where it applies and the unspecific, where not.

${Url.For({...@controller: 'someController', @action: 'some Action', @params:
{...@key: 'specialKey'}}) should render '/some-funky-route', if I have a
specific route with this param and should render
/someNormalRoute/forThisAction/<key>, for all other cases.

Any ideas?

2009/11/18 Jan Limpens <[email protected]>

> as expected this does not work - i cannot simply inject parameters that
> way. I try to analyze PatternRoute to see, where some matching <> or []
> parameter is somehow passed on somewhere. But I think this is not happening
> there. The route just matches and creates an Url. During the Match()ing the
> RouteMatch |Params get filled with something and probably picked up on the
> Controller/Action side.
>
> So in my case the Match.Params receive nothing, because I have no parameter
> in the pattern.
>
> I will try to subclass PatternRoute and add parameters 'manually' there...
>
> 2009/11/18 Jan Limpens <[email protected]>
>
> so if i have a custom routing defined by a xml file like this:
>>
>>     <route matches="/artigos-para-meninos-de-todos-tamanhos"
>> controller="Articles" action="ListByCategory">
>>         <params>
>>             <param patternPart="key" value="para-meninos"/>
>>         </params>
>>     </route>
>>
>> this should get me there? (i have not tried it yet, but it seems a bit
>> suspicious to me... the fact that i have no namedPatternparts)
>>
>> prototype code:
>>
>>         private static IRoutingRule makeRoute(XElement xElement)
>>         {
>>             var controllerAttr = xElement.Attribute("controller");
>>             var controllerType = controllerAttr != null ?
>> controllerAttr.Value : null;
>>             var actionAttr = xElement.Attribute("action");
>>             var action = actionAttr != null ? actionAttr.Value : null;
>>             var patternAttr = xElement.Attribute("pattern");
>>             var pattern = patternAttr != null ?
>> patternAttr.Value.Replace('{', '<').Replace('}', '>') : null;
>>             var route = new PatternRoute(pattern)
>>                 .DefaultForAction().Is(action)
>>                 .DefaultForController().Is(controllerType);
>>             var paramElements = xElement.Descendants().Where(e =>
>> e.NodeType == XmlNodeType.Element && e.Name == "params").ToList();
>>             if (paramElements.IsNullOrEmpty())
>>                 return route;
>>             foreach (var paramElement in paramElements)
>>             {
>>                 var attrs = new[] { paramElement.Attribute("patternPart"),
>> paramElement.Attribute("value") };
>>                 if (attrs.Any(e => e == null))
>>                     continue;
>>                 route.DefaultFor(attrs[0].Value).Is(attrs[1].Value);
>>             }
>>             return route;
>>         }
>>
>>
>> 2009/11/13 Ken Egozi <[email protected]>
>>
>> try Default.For(Whichever).Is(Whatever)
>>>
>>> On Fri, Nov 13, 2009 at 12:56 AM, Jan Limpens <[email protected]>wrote:
>>>
>>>> but how do I pass a parameter to the action, then?
>>>>
>>>> 2009/11/10 hammett <[email protected]>
>>>>
>>>>> Hmm.. IIRC if you dont specify a pattern to a PatternRoute, all route
>>>>> nodes will be required, so you'll achieve exactly what your boss wants.
>>>>>
>>>>> On Mon, Nov 9, 2009 at 2:50 PM, Jan Limpens <[email protected]>wrote:
>>>>>
>>>>>> Hello!
>>>>>>
>>>>>> for my FooController/BarAction(string val) I have this routing:
>>>>>>
>>>>>> "/foo/bard/<val>"
>>>>>>
>>>>>> Now my boss enters and tells me: "For the val 'xyz', I want this other
>>>>>> routing '/my-wonderful-routing-for-xyz' and for 'abc', I want
>>>>>> '/this-is-a-nice-abc', the rest is nice the way it is."
>>>>>>
>>>>>> And I have no good idea how to implement this easily. I miss a way in
>>>>>> PatternRoute to pass in a parameter and this makes sense - because it is 
>>>>>> a
>>>>>> PatternRoute it expects a pattern, not something concrete. Is there an 
>>>>>> easy
>>>>>> way to implement something like a ConcreteRule, or do I have to do this 
>>>>>> all
>>>>>> by myself?
>>>>>>
>>>>>> --
>>>>>> Jan
>>>>>>
>>>>>> --~--~---------~--~----~------------~-------~--~----~
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Castle Project Users" group.
>>>>>> To post to this group, send email to
>>>>>> [email protected]
>>>>>> To unsubscribe from this group, send email to
>>>>>> [email protected]<castle-project-users%[email protected]>
>>>>>> For more options, visit this group at
>>>>>> http://groups.google.com/group/castle-project-users?hl=en
>>>>>> -~----------~----~----~----~------~----~------~--~---
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Cheers,
>>>>> hammett
>>>>> http://hammett.castleproject.org/
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Castle Project Users" group.
>>>>> To post to this group, send email to
>>>>> [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]<castle-project-users%[email protected]>
>>>>> .
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/castle-project-users?hl=.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Jan
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Castle Project Users" group.
>>>> To post to this group, send email to
>>>> [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<castle-project-users%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/castle-project-users?hl=.
>>>>
>>>
>>>
>>>
>>> --
>>> Ken Egozi.
>>> http://www.kenegozi.com/blog
>>> http://www.delver.com
>>> http://www.musicglue.com
>>> http://www.castleproject.org
>>> http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Castle Project Users" group.
>>> To post to this group, send email to
>>> [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<castle-project-users%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/castle-project-users?hl=.
>>>
>>
>>
>>
>> --
>> Jan
>>
>
>
>
> --
> Jan
>



-- 
Jan

--

You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=.


Reply via email to