MVC Routing in 5.1

Posted by Community Admin on 03-Aug-2018 23:47

MVC Routing in 5.1

All Replies

Posted by Community Admin on 20-Jul-2012 00:00

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

Posted by Community Admin on 23-Jul-2012 00:00

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>

Posted by Community Admin on 23-Jul-2012 00:00

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 :)

Posted by Community Admin on 23-Jul-2012 00:00

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

Posted by Community Admin on 23-Jul-2012 00:00
Posted by Community Admin on 23-Jul-2012 00:00

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.

Posted by Community Admin on 23-Jul-2012 00:00

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

Posted by Community Admin on 23-Jul-2012 00:00

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!

Posted by Community Admin on 23-Jul-2012 00:00

yeah, I'm doing that right now ;)

Posted by Community Admin on 23-Jul-2012 00:00

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.

Posted by Community Admin on 23-Jul-2012 00:00

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

Posted by Community Admin on 23-Jul-2012 00:00

"with few other things"

<fingerscrossed>Widgets, Widgets, Widgets, Widgets</fingerscrossed>

Posted by Community Admin on 23-Jul-2012 00:00

@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.

This thread is closed