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.
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);
}
}
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/....?
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?
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.
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
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:
You can call that by going to the URL:
or
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
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 justMySurface/
- just a typo :)Have you read https://our.umbraco.org/documentation/Reference/Routing/surface-controllers ?
Didn't compile it - dummy!
Many thanks.
Roger
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?
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/
is working on a reply...