Copied to clipboard

Flag this post as spam?

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


  • Roger Withnell 128 posts 613 karma points
    Aug 25, 2016 @ 18:02
    Roger Withnell
    0

    Call a controller from a URL

    In my application, I want to put a URL in an email and, when the recipient of the email clicks this URL, it runs some code which sends an email back to the same recipient.

    I'm putting the code in a controller and the whole thing works but how do I call the controller with a URL? I've tried a regular controller and a surface controller to no avail.

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 25, 2016 @ 18:11
    Dan Diplo
    1

    How do you mean "call the controller with a URL"? If you create, say, a surface controller and you access the URL from a browser it will be called. For instance, take the following simple example controller:

    public class MySurfaceController : SurfaceController
    {
        public ActionResult DoSomething()
        {
            return Content("I've done something!");
        }
    
        public ActionResult DoSomethingWithParam(int userId)
        {
            return Content("I've done something for " + userId);
        }
    }
    

    You can call that by going to the URL:

    / Umbraco/Surface/MySurfaceSurface/DoSomething
    

    or

    / Umbraco/Surface/MySurfaceSurface/DoSomethingWithParam?userId=20
    
  • Roger Withnell 128 posts 613 karma points
    Aug 25, 2016 @ 18:32
    Roger Withnell
    0

    Thanks, Dan, but this doesn't work for me. I get a 404 error

    Couple of questions: 1. I've put my surface controller in the Controller folder. Correct? 2. Your controller's name is MySurfaceController but in the URL you have ..../MySurfaceSurface/.... Should this be ..../MySurface/....?

    Roger

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 25, 2016 @ 18:36
    Dan Diplo
    101

    Your controller needs to compiled, but it doesn't really matter where it goes. You can put it in App_Code or add it to a DLL. Umbraco should find it.

    Yes, MySurfaceSurface/ should have been just MySurface/ - just a typo :)

    Have you read https://our.umbraco.org/documentation/Reference/Routing/surface-controllers ?

  • Roger Withnell 128 posts 613 karma points
    Aug 25, 2016 @ 18:39
    Roger Withnell
    0

    Didn't compile it - dummy!

    Many thanks.

    Roger

  • Tim C 161 posts 528 karma points
    Aug 27, 2016 @ 16:11
    Tim C
    0

    I'm using Umbraco in a hosted environment and haven't yet been able to compile Umbraco locally (there seem to be some issues with incorrect files). If I just put a controlled in App_code would it automatically compile when called the first time?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 27, 2016 @ 20:24
    Dan Diplo
    0

    Yep, you can put controllers in App_Code and they'll be compiled when accessed for the first time. I think it's a good idea to put them in a namespace, too.

    NB. I've a package that (amongst other things) will show you what controllers you have in a site etc. - https://our.umbraco.org/projects/developer-tools/diplo-god-mode/

Please Sign in or register to post replies

Write your reply to:

Draft