Copied to clipboard

Flag this post as spam?

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


  • James Martens 2 posts 72 karma points
    Mar 22, 2018 @ 01:57
    James Martens
    0

    Best Approach for a custom contact form

    Hi

    I am a relative .Net/Umbraco noob, coming from Ruby on Rails land. I have what is probably a very simple problem to solve, but I'd like to solicit some of the community's advice before I go too far down any rabbit holes so to speak.

    It's the age old contact form problem - I need to have a form with an email address, name and comment fields, and I want to fire these 3 fields off to Mailgun when the form is submitted.

    Should I be using a SurfaceController for this? I just want to update the form with messaging when it completes the request, not redirect the user to a new page or anything. The form itself is plain old HTML right now, but we have a react.js layer so I would ideally prefer a JSON response from whatever this controller does.

    My attempt to use a surface controller so far and testing it is running up against an error:

    Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request
    

    My controller code is as follows:

    using Ecommerce.Web.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using RestSharp;
    using RestSharp.Authenticators;
    
    namespace Ecommerce.Web.Controllers{
    
      public class ContactsController : SurfaceController
      {
        [HttpPost]
        public ActionResult Submit(ContactFormViewModel model) 
        {
      if (!ModelState.IsValid)
      return CurrentUmbracoPage();
    
    RestClient client = new RestClient();
    client.BaseUrl = new Uri("https://api.mailgun.net/v3");
    client.Authenticator =
    new HttpBasicAuthenticator("api",
    "secret-keys-yo);
    RestRequest request = new RestRequest();
    request.AddParameter("domain", "somedomain", ParameterType.UrlSegment);
    request.Resource = "{domain}/messages";
    request.AddParameter("to", "[email protected]");
    request.AddParameter("from", "[email protected]");
    request.AddParameter("subject", "Website Inquiry");
    request.AddParameter("text", "Test message");
    request.Method = Method.POST;
    var something = client.Execute(request);
      //parse form data here
      return RedirectToCurrentUmbracoPage();
        }
      }
    }
    

    I'm testing this endpoint by simply issuing a POST to http://localhost:25029/umbraco/surface/Contacts/Submit, which is where I see my 500 error showing up. To me that indicates that the routing is working as expected, but my knowledge of this ecosystem is insufficient to diagnose the underlying issue.

    A final point of consideration: I am not persisting any of this information locally. For this reason I have only created a controller to do the work; dispatch the payload to mailgun. There is no model.

    Just looking for tips and pointers here.

    Thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 23, 2018 @ 09:45
    Dave Woestenborghs
    0

    How are you rendering your form in your view ?

    Dave

  • Harry Spyrou 212 posts 604 karma points
    Mar 23, 2018 @ 12:27
    Harry Spyrou
    0

    there's also a discussion about this here:

    Disucssion

  • James Martens 2 posts 72 karma points
    Mar 23, 2018 @ 17:39
    James Martens
    0

    Hi

    I did a little bit more remedial reading on MVC and Surface Controllers in general and have more or less gotten past this part. I had, earlier, tried to infer some things about routing and my initial assumptions turned out to be incorrect. Also, issuing a post from Postman without the antiforgery token probably contributed to this misbehaving initially. TIL.

    Right now I am rendering the form via a partial, and it is submitting to a surface controller.

    I did end up making a view model for starters, it's just easier to handle things with one than without. I have it working as a Postback for now and am trying to get it working via Ajax, since the client wishes it to function that way.

    Based on other threads here and on Stack Overflow it looks like it would be possible to use either an API controller OR a surface controller to handle an Ajax post, though my impression is that the community favors the Api controller approach for this task.

Please Sign in or register to post replies

Write your reply to:

Draft