Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tito 314 posts 623 karma points
    Jul 08, 2014 @ 17:25
    Tito
    0

    Error hijacking umbraco routes

    Im using Umbraco 7.1.4 and want to hijack a route to a Document Type alias "Pagina".

    I am following this post: 

    http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers

    So i have created this class:

    namespace WebUmb.Controllers

    {

        public class PaginaController : Umbraco.Web.Mvc.RenderMvcController

        {

            public override ActionResult Index(RenderModel model)

            {

                //Do some stuff here, then return the base method

                return base.Index(model);

            }

     

        }

    }

    This is the error:

    System.InvalidOperationException: Multiple types were found that match the controller named 'Pagina'. This can happen if the route that services this request ('umbraco/RenderMvc/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

    The request for 'Pagina' has found the following matching controllers:
    PaginaController
    PaginaController

    I have tried without namespace and with the template alias in the action instead of "Index", but it gets the same error. 


  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 09, 2014 @ 09:22
    Sebastiaan Janssen
    0

    It sounds like you have two PaginaController classes in your codebase? Also, if this is something you're trying in AppCode, try to use a compiled class instead of the AppCode file.

  • Tito 314 posts 623 karma points
    Jul 09, 2014 @ 10:10
    Tito
    0

    Yes! that was the problem! i thought i had tried that out. I moved the controllers folder outside the app_code folder and recompiled the project. Now it works.

    One more question, now i have

    public override ActionResult Index(RenderModel model)

    and the url 

    /somepage

    if i want the action to be:

    public override ActionResult Index(RenderModel model, string id)

    and the url to be

    /somepage/id

    it does not work, how could i do it?

    Thanks!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 09, 2014 @ 10:18
    Sebastiaan Janssen
    0

    That I don't know. :-) I'll ask Shannon if he can have a look.

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jul 09, 2014 @ 10:32
    Shannon Deminick
    1

    Custom URLs are different than hijacking a route. Some things to note:

    • When a request comes in on a URL, Umbraco needs to map that route to a node, if a url doesn't map to a node than there is no way that Umbraco knows what it is
    • Therefore, if something like /somepage/id does not match a node in the Umbraco tree then how would Umbraco know that it is a node?

    There's a few ways to handle what you want:

    • IContentFinder - this is how Umbraco maps a URL to a content item. http://our.umbraco.org/documentation/Reference/Request-Pipeline/IContentFinder , since you look to be wanting to have your id show up as an Action's parameter, then this is probably not for you but it's worth noting this is the easiest way to relate a url to an Umbraco node.
    • Instead of having the url like /somepage/id you could do /somepage?id=blah, then in your controller you'd do:

      [NonAction]
      public override ActionResult Index(RenderModel model)
      {}
      
      
      public ActionResult Index(RenderModel model, string id)
      {
          ...
      }
      

    But you probably don't want query strings and would rather have MVC style URLs, so there's only 2 options left:

    • Using [EnsurePublishedContentRequest] and custom MVC routes
      • You'd create a normal MVC route to match the URL you want (you can create MVC routes dynamically based on some found content in the tree, like your 'somepage' node), or you can just hard code routes.
      • Then on your action you'd use the [EnsurePublishedContentRequest] attribute to tell Umbraco what IContent item you'd like to associate with the request - this is so that Umbraco knows this current request is actually for an Umbraco node.
      • This is all based off my old blog post here: http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco
    • The last option is something new but is only available in 7.1.5 or 6.2.2 which haven't been released yet, BUT you can get this working yourself.

    I'll properly document this all eventually.

  • Tito 314 posts 623 karma points
    Jul 09, 2014 @ 12:19
    Tito
    0

    Thanks Shannon!!

    The first part works great, with /somepage?id=param

    url /somepage relates ok to the somepage node in umbraco, note that the id is not a node, "somepage" is the node, so the umbraco url for the node is /somepage. I only want to pass a parameter ?id=param in the mvc form: /somepage/id.

    So i was thinking about using url rewriting for the /somepage/?id=param rewrite to /somepage/id, may be it would be an easier solution than using [EnsurePublishedContentRequest].

    First i thought about doing it in the web.config rewrite section but didnt know how to implement it, so i have written a maproute:

    RouteTable.Routes.MapRoute(

                  name: "Route",

                 url: "somepage/{id}",

                  defaults: new { controller = "Pagina", action = "somepage", id = UrlParameter.Optional }

                );

    The action gets executed, but the RenderModel is null. I think umbraco does not relate the url to the node content, but i dont know why as it should be invisible to umbraco as the map route shoud transform somepage/id to somepage?id=

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jul 10, 2014 @ 09:03
    Shannon Deminick
    0

    @Tito, yup so you need to use one of the last 2 options i listed above with custom routing.

Please Sign in or register to post replies

Write your reply to:

Draft