concrete routing

3 views
Skip to first unread message

Jan Limpens

unread,
Nov 9, 2009, 5:50:47 PM11/9/09
to castle-project-users
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

hammett

unread,
Nov 10, 2009, 12:54:11 PM11/10/09
to castle-pro...@googlegroups.com
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.

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




--
Cheers,
hammett
http://hammett.castleproject.org/

Jan Limpens

unread,
Nov 12, 2009, 5:56:03 PM11/12/09
to castle-pro...@googlegroups.com
but how do I pass a parameter to the action, then?

2009/11/10 hammett <ham...@gmail.com>

--

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



--
Jan

Ken Egozi

unread,
Nov 12, 2009, 9:12:09 PM11/12/09
to castle-pro...@googlegroups.com
try Default.For(Whichever).Is(Whatever)
Ken Egozi.
http://www.kenegozi.com/blog
http://www.delver.com
http://www.musicglue.com
http://www.castleproject.org
http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Jan Limpens

unread,
Nov 18, 2009, 12:28:05 PM11/18/09
to castle-pro...@googlegroups.com
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 <ego...@gmail.com>



--
Jan

Jan Limpens

unread,
Nov 18, 2009, 2:33:21 PM11/18/09
to castle-pro...@googlegroups.com
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 <jan.l...@gmail.com>



--
Jan

Jan Limpens

unread,
Nov 18, 2009, 5:10:08 PM11/18/09
to castle-pro...@googlegroups.com
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 <jan.l...@gmail.com>



--
Jan
Reply all
Reply to author
Forward
0 new messages