Copied to clipboard

Flag this post as spam?

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


  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 12, 2012 @ 07:10
    Peter Gregory
    1

    Calling Actions on a Controller via URL using MVCBridge

    Today I wanted to build a form that used utilised the MVC Remote attribute on the Model.  Unfortunately this does not work straight out of the box because it is not able to create the URL to the action to perform the validation.  However with a little bit of work you can get this working again.

    First thing you need to know is that you will need to tell the application about the routes to the Controller.  Do this by creating a class in your App_Start folder in your MVC application that has your MVCBridge work in it.  I called mine ControllerRouting.cs

    Its a pretty simple class for registering rew Routes in the RouteTable and should look a little something like this.

    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    [assembly: PreApplicationStartMethod(typeof(MyApplication.App_Start.ControllerRouting), "Setup")]
    namespace MyApplication.App_Start
    {
       public class ControllerRouting
        {
           public static void Setup()
            {
               RouteTable.Routes.MapRoute(
                   "SimpleRoute", // Route name call it anything you want
                   "Simple/{action}/{id}", // URL with parameters,
                   new{ controller = "Simple",action="Do",id=UrlParameter.Optional}
                   );
          }
        }
    }

    The above assumes you have a Controller somewhere in your MVCBridge project called SimpleController, and that it has an Action called Do.  The reason that the Controller name is hardcoded is because otherwise the MVC portion of the application would take over completely and you would not be able to get to normal Umbraco pages. More information on this can be found here : http://www.aaron-powell.com/umbraco/using-mvc-in-umbraco-4-revisited and information on how to run an MVC application alongside Umbraco can be found here : http://www.aaron-powell.com/umbraco/using-mvc-in-umbraco-4

    Basically when you want to call a controller like you would in a standard MVC application in a the Umbraco Web Forms environment you need treat it like its an MVC application running along side Umbraco.

    In my case I wanted to do Remote Validation in an MVC form that was being served through the MVCBridge Macro.  Unfortunately it does not work straight up because it has no idea how to access the controller and it cannot create the URL in the Validation JS that the jQuery validate plugin needs.  But by doing the above I was able to get it to work and its a beautiful thing :)

    I hope this helps someone.

  • Richard Boelen 61 posts 153 karma points
    Sep 12, 2012 @ 09:48
    Richard Boelen
    0

    Hi,

    thanks for sharing!

    Cheers!

  • Vinod 22 posts 83 karma points
    Jan 25, 2014 @ 19:24
    Vinod
    0

    Hi, 

     

    Thanks for sharing. But the problem I found with this is that the when I add another controller then I have to rewrite the above peice of code with that controller name and different route name. if i am not defining each controller the routename its not working for me.. Example,I created two controller ProductController and HomeController. Now I add just the routename for home its wroking but when i take a url with product its giving me error. So what i did is I found all the controllers in that folder and then in the for loop i have checked the count. till the count ends i am creating new route tables.

    usingSystem.Web;
    usingSystem.Web.Mvc;
    usingSystem.Web.Routing;

    [assembly:PreApplicationStartMethod(typeof(MyApplication.App_Start.ControllerRouting),"Setup")]
    namespaceMyApplication.App_Start
    {
       
    publicclassControllerRouting
       
    {
           
    publicstaticvoidSetup()
           
    {
               
    RouteTable.Routes.MapRoute(
                   
    "SimpleRoute",// Route name call it anything you want
                   
    "Simple/{action}/{id}",// URL with parameters,
                   
    new{ controller ="Simple",action="Do",id=UrlParameter.Optional}
                   
    );
           
    }
       
    }}

    Is that the correct way to go? Next I tried by giving as common,

    RouteTable.Routes.MapRoute(
                   
    "SimpleRoute",// Route name call it anything you want
                   
    "{controller}/{action}/{id}",// URL with parameters,
                   
    new{ controller ="Simple",action="Do",id=UrlParameter.Optional}
                   
    );

    But now I am able to access the MVC  link but the umbraco links are not coming..just a blank screen is shown on clicking the umbraco url.

Please Sign in or register to post replies

Write your reply to:

Draft