Is it possible to use parameters from the URL in MVC Actions?
Hi, I'm experimenting with the MVC support in Umbraco, and have been reading through the documentation on MVC support and read alot of blog posts on it. However, I haven't seen any examples of using parameters on the actions, taken from the URL. For example, I would like to have a MVC Action that responded to "/User/32", where my Action method would be "User" and "32" would be the parameter to that Action.
But I haven't been able to do that, since Umbraco returns 404 for "/User/32", since there's no page with that name. If there's no support for this at the moment, do you think it would be possible to make my own custom 404 handler that would look at the URL at see if there's a MVC view further down the URL chain? So /User/32" would call my 404 handler, and that handler would check if there's a MVC view at "/User/", and if there is, call it with the rest of the url as a parameter.
I should add that I don't want to add custom routes via code. (A least not 'static' custom routes) I would like the MVC Action to be bound to the document type, as documented in the article "Hijacking Umbraco routes", so if the CMS user decides to rename a folder in the path of the MVC Action, it would still work without having to edit a route in code.
I'd be curious in an answer to this one as well. I have a certain data type of content stored under a folder/node that is hidden from navigation etc. I do this because I enable content editors to create a number of these data types in this folder and then add them to various pages. Some pages are a list of these items and I'd like to have a detail page where more info can be displayed while maintaining the url.
ie. url looks like this
/staff/persondatatype (this is the hidden node with many persondatatype entreis)
/teammembers/ (page lists a subset of persondatatype objects)
/teammembers/personname (would like to be able to display content from the persondatatype object here using specified template).
I've been able to add a custom controller that fires ActionResult Index(RenderModel model) when /teammembers/ is hit but that's where I'm stumped, how do I add the /personname to the url without getting a 404 and then load the data into a custom template.
Am relatively new to Umbraco as well so any help is greatly appreciated!
I looked into this yesterday, and it seems that custom 404 handlers has been deprecated. Instead, one should implement a class which implements the interface IPublishedContentLookup in the namespace Umbraco.Web.Routing.
There are some classes in Umbraco that already does this, and my plan is to figure out which one is used to figure out to call a MVC Action with a specific URL. I think it's the class LookupByNiceUrl, but I'm not 100% certain yet.
My plan is then to code some logic that removes one part of the url at a time, and check if LookupByNiceUrl finds any MVC Action at that URL. If it does, I will call it and use the rest of the URL as parameters to the MVC Action. Hopefully, I will be able to reuse some of the builtin logic in ASP.NET MVC to automatically convert the parameters to the correct types that the MVC Action expects.
Unfortunately, I haven't had the time to dig any more into this.
We changed our design, so instead of having the MVC project live inside of Umbraco, we're hosting it outside of Umbraco, which means there's no longer any problems with the routing.
So the question still stands.
However, for your paging parameters, maybe you could use a client-side approach and load the data from a web service, and combine it with pushstate or hashbang URLs?
Has anyone had success with this concept? It seems pretty common and fundamental yet there is next to no documenation on this. I've had success wiring a SurfaceControler (requires specific Template) and a CustomController (requires specific doctype). Both of these methods however require that the actual document exists in the content tree. For the scenario I'm after, for example a simple gallery that pulls images from a media folder, I'd like to have a url like:
www.sitename.com/photos/galleryname
The /photos/ page exists using an existing doctype and template, it pulls a list of media sub folders from a specified top level folder. The trick comes when I want to be able to click on a folder to display it's contents, giving me a url of either /photos/galleryname or /photos/1234 (ID). My understanding here is that a custom route is required but the documentation here is minimal at best http://our.umbraco.org/documentation/reference/mvc/custom-routes
I then have a controller called PhotoGalleryController but it never gets called, unless of course I'm looking up a page that exists in the content tree and has a doc type of "PhotoGallery"
So it seems there's a missing link here, both in my knowledge and in the documentation how to to pull this off. I have a whole bunch of scenarios where this type of functionality would be really handy and based on google searches on the subject it seems like there are a lot of other people out there looking as well, just no solutions...
As always, any help is greatly appreciated, and sample code is appreciated even more!
Is it possible to use parameters from the URL in MVC Actions?
Hi, I'm experimenting with the MVC support in Umbraco, and have been reading through the documentation on MVC support and read alot of blog posts on it. However, I haven't seen any examples of using parameters on the actions, taken from the URL. For example, I would like to have a MVC Action that responded to "/User/32", where my Action method would be "User" and "32" would be the parameter to that Action.
But I haven't been able to do that, since Umbraco returns 404 for "/User/32", since there's no page with that name. If there's no support for this at the moment, do you think it would be possible to make my own custom 404 handler that would look at the URL at see if there's a MVC view further down the URL chain? So /User/32" would call my 404 handler, and that handler would check if there's a MVC view at "/User/", and if there is, call it with the rest of the url as a parameter.
I should add that I don't want to add custom routes via code. (A least not 'static' custom routes) I would like the MVC Action to be bound to the document type, as documented in the article "Hijacking Umbraco routes", so if the CMS user decides to rename a folder in the path of the MVC Action, it would still work without having to edit a route in code.
I'd be curious in an answer to this one as well. I have a certain data type of content stored under a folder/node that is hidden from navigation etc. I do this because I enable content editors to create a number of these data types in this folder and then add them to various pages. Some pages are a list of these items and I'd like to have a detail page where more info can be displayed while maintaining the url.
ie. url looks like this
/staff/persondatatype (this is the hidden node with many persondatatype entreis)
/teammembers/ (page lists a subset of persondatatype objects)
/teammembers/personname (would like to be able to display content from the persondatatype object here using specified template).
I've been able to add a custom controller that fires ActionResult Index(RenderModel model) when /teammembers/ is hit but that's where I'm stumped, how do I add the /personname to the url without getting a 404 and then load the data into a custom template.
Am relatively new to Umbraco as well so any help is greatly appreciated!
I looked into this yesterday, and it seems that custom 404 handlers has been deprecated. Instead, one should implement a class which implements the interface IPublishedContentLookup in the namespace Umbraco.Web.Routing.
There are some classes in Umbraco that already does this, and my plan is to figure out which one is used to figure out to call a MVC Action with a specific URL. I think it's the class LookupByNiceUrl, but I'm not 100% certain yet.
My plan is then to code some logic that removes one part of the url at a time, and check if LookupByNiceUrl finds any MVC Action at that URL. If it does, I will call it and use the rest of the URL as parameters to the MVC Action. Hopefully, I will be able to reuse some of the builtin logic in ASP.NET MVC to automatically convert the parameters to the correct types that the MVC Action expects.
How did you get on with this? I have a similar issue which is causing me to tear my hair out here...
http://our.umbraco.org/forum/developers/api-questions/38048-Umbraco-411-MVC-Custom-Routing-Content-is-null-How-can-I-load-content
I've got paging parameters that I need to handle
Unfortunately, I haven't had the time to dig any more into this.
We changed our design, so instead of having the MVC project live inside of Umbraco, we're hosting it outside of Umbraco, which means there's no longer any problems with the routing.
So the question still stands.
However, for your paging parameters, maybe you could use a client-side approach and load the data from a web service, and combine it with pushstate or hashbang URLs?
Has anyone had success with this concept? It seems pretty common and fundamental yet there is next to no documenation on this. I've had success wiring a SurfaceControler (requires specific Template) and a CustomController (requires specific doctype). Both of these methods however require that the actual document exists in the content tree. For the scenario I'm after, for example a simple gallery that pulls images from a media folder, I'd like to have a url like:
www.sitename.com/photos/galleryname
The /photos/ page exists using an existing doctype and template, it pulls a list of media sub folders from a specified top level folder. The trick comes when I want to be able to click on a folder to display it's contents, giving me a url of either /photos/galleryname or /photos/1234 (ID). My understanding here is that a custom route is required but the documentation here is minimal at best http://our.umbraco.org/documentation/reference/mvc/custom-routes
I'm not sure but I thought Keith might be on the right track here http://our.umbraco.org/forum/developers/api-questions/38048-Umbraco-411-MVC-Custom-Routing-Content-is-null-How-can-I-load-content but it doesn't seem that he's got a solution yet either. Following his process I added a route to global.ascx that gets fired. It looks like this
routes.MapRoute(
"PhotoGallery", "photos/{gallery}",
new { controller = "PhotoGallery", action = "Index", gallery = UrlParameter.Optional },
new { gallery = @"\d+" });
I then have a controller called PhotoGalleryController but it never gets called, unless of course I'm looking up a page that exists in the content tree and has a doc type of "PhotoGallery"
So it seems there's a missing link here, both in my knowledge and in the documentation how to to pull this off. I have a whole bunch of scenarios where this type of functionality would be really handy and based on google searches on the subject it seems like there are a lot of other people out there looking as well, just no solutions...
As always, any help is greatly appreciated, and sample code is appreciated even more!
Regards,
Phill
I'm interested in methods to do this as well. I don't want to use a static route or query strings.
is working on a reply...