+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / MVC Routing in 5.1

MVC Routing in 5.1

13 posts, 1 answered
  1. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    20 Jul 2012
    Link to this post
    I have a controller in 5.1 MVC with 2 ActionResults:

    Index()
    and
    Details(string product)

    Each of them gets hit when I would expect (eg /mypage/ and /mypage/details/product1), but the "product" parameter (in this case "product1") never gets assigned - it shows as null when debugging.

    Do I have to do anything to set up routes? I downloaded the music store example and see that routing in that works as expected (eg Browse(string Genre)), and I don't see anything explicitly setting routes in there, so I'm not sure.

    Thanks in advance

    Jonathan
  2. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    23 Jul 2012
    Link to this post
    By default, ASP.NET MVC, has a numeric (int) parameter called 'id' that would typically be used in this instance. Your details method would look like this:

    Details(int id)

    And your route would look like this:

    /mycontroller/Details/1

    If you want something different than that, you will either need to modify the route, or use named parameters. But that said, I have not yet looked at the Record Store example. I suspect they used the named parameters as it is generally more flexible than creating new routes all over the place. Here is an example of how you would build a URL for your example:

    // the new {} object contains any needed named parameters for the url.
    // the helper will automatically build a URL according to the best matching URL rules.
    <
    a href="@Url.Action("Details", "ControllerName", new { product = "product1"})">Product 1</a>
  3. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    23 Jul 2012
    Link to this post
    Yeah, ivan (in the videos) uses UrlName as the value...but that brings up a question I have going through that sample

    I don't want my url to look like

    /recipies/Details/cookie
    (with Details being the view)

    I want it just
    /recipies/cookie

    ...and Details shouldn't be uppercase

    (I'm clearly not an MVC expert :)
  4. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    23 Jul 2012
    Link to this post
    Hi Dan

    Thanks for the reply

    In my case, I need a string value passed through, so "id" won't work.

    In the record store sample, in the StoreController, there is a "Browse" controller that accepts a string input:

    public ActionResult Browse(string Genre)

    Now I would either edit a route in global.asax (MVC3) or in App_Start/RouteConfig.cs (MVC4), but I don't see either of them in there. (I believe SF 5.1 is using MVC3)

    Examining the record store demo, there is also no global.asax, or indeed any route configurations that I can see, and yet that method gets its parameter just fine when I visit (eg) /store/Browse/Classical/

    I can visit (on my site) /mypage/details?product=product1 and get the parameter passed, but I don't want to use query string parameters, I want to use the proper MVC approach

    Thanks in advance

    Jonathan
  5. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
  6. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    23 Jul 2012
    Link to this post
    As I suspected, the view uses 'named parameters' in the URL which is constructed with a typical helper. (the named parameters are the items inside new { ...parameters here... })

    @model IEnumerable<SitefinityWebApp.Mvc.Models.Genre>
    ...
        @foreach (var genre in Model)
        {
            <li>@Html.ActionLink(genre.Name, "Browse", new { Genre = genre.Name})</li>
        }

    This matches up with the controller action:

    public ActionResult Browse(string Genre) ...

    Now that we know the mechanism that is being used, we are back to your question: Is Sitefinity some how magically creating the route you need? ...or are they just hiding the route config in some non-standard location?

    Note: if someone knows what mechanism 5.1 uses to determine routing, I would love to know.

  7. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    23 Jul 2012
    Link to this post
    Oh my gosh I figured out my issue!

    Turns out the action name in the url is *sort of* case-sensitive (unlike standard MVC). That is, /ACTION/product1, /Action/product1, and /action/product1 all hit the same action, but only the one with the correct case passes through parameters correctly. On all others, the action is hit, but "product" is null.

    This seems like a bug to me.

    Thanks both of you for your help - that video showed me I wasn't going crazy

    Jonathan
  8. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    23 Jul 2012
    Link to this post
    If you think it's a bug please please report it :)  The powers that be might not be monitoring this thread and so we'd want this in on the next SP if possible!
  9. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    23 Jul 2012
    Link to this post
    yeah, I'm doing that right now ;)
  10. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    23 Jul 2012
    Link to this post
    Seriously?! Well I am glad you found it then. I will test that when I get my site updated from 5.0 to 5.1 as well.

    If that is the case, we REALLY need to understand where the routing is done. Either we need to locate the config file, the DLL that does the routing, or have someone at Telerik document the MVC routing. MVC controllers are highly dependent upon the routes. I don't tend to change my MVC routes, but I know how they are configured.
  11. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    23 Jul 2012
    Link to this post
    Hello,

    I am sorry that I have only now seen this thread and would like to apologize to all.

    We have found this issue, and yes indeed, it is a bug. We already have a fix in place and we'll release it sometime next week - with few other things.

    As, for how the routing works - here is a very brief explanation. Sitefinity uses routing engine since 4.0 (so, for quite a long). When you are working in pure or hybrid modes, the route is actually picked up by the page. The page then constructs dynamically new routes for the MVC based widgets - which may or may not respond to it. If at least one of the widgets can handle the route data, page will render - otherwise you'll get 404. In a word - you should use route registrations only for classic MVC (side by side apps) mode.

    Regards,
    Ivan
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  12. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    23 Jul 2012
    Link to this post
    "with few other things"

    <fingerscrossed>Widgets, Widgets, Widgets, Widgets</fingerscrossed>
  13. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    23 Jul 2012
    Link to this post
    @Ivan: In regard to Steve's query about changing the route from:

    /recipies/Details/cookie
    (with Details being the view)

    To this:
    /recipies/cookie

    If I understand you correctly, this would not work out of the box, because the routing engine within the page will need to know about the Details view.

    I personally am fine with that for now, I just want to understand 'how' it works so I can work with it.

    Thanks for the reply. That helped clear up a few things for me.
13 posts, 1 answered