Copied to clipboard

Flag this post as spam?

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


  • Alex Perotti 53 posts 94 karma points
    Mar 02, 2015 @ 22:43
    Alex Perotti
    0

    Routing Surface Controller

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    public class ValidationSurfaceController : SurfaceController
    {
        public JsonResult EmailIsUnique(string email)
        {
            return this.Json(true);
        }
    }
    

    This is my controller (will be used with a Remote data annotation).

    My problem is that the url /umbraco/surface/ValidationSurface/EmailIsUnique/ (that seems to be the expected one) and /umbraco/surface/Validation/EmailIsUnique both returns a 404 error

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Mar 03, 2015 @ 05:09
    Nicholas Westby
    0

    Some notes about surface controllers:

    • Surface controllers are routed with a hidden field value rather than with a URL. I believe it has a name of "ufprt". I think you can use pretty much any URL so long as that hidden field is passed with the request.
    • What makes you think the correct URL would start with /umbraco/surface? On a related note, what version of Umbraco are you using?
    • Are you doing a GET request or a POST request? You may want to mark your action method with an AcceptVerbs attribute, specifying both GET and POST: http://www.dotnetexpertguide.com/2012/11/aspnet-mvc-acceptverbs-action-filter.html
    • When returning a JSON result from a GET request, you typically have to pass in a second parameter to the Json function to allow GET requests.

    Usually looks like this:

    return Json(data, JsonRequestBehavior.AllowGet)
    

    That's not necessary if you are doing POST requests.

  • Alex Perotti 53 posts 94 karma points
    Mar 03, 2015 @ 08:36
    Alex Perotti
    0

    Thank you Nicholas.

    I guessed the url following the documentation

    I fixed the controller with the "AllowGet" behavior and now I can get it at /umbraco/surface/ValidationSurface/EmailIsUnique/

    Unfortunately the Remote annotation still doesn't work

    [Remote("EmailIsUnique", "ValidationSurfaceController", HttpMethod = "GET", ErrorMessage = "UNIQUE")]
    

    It throws an exception when rendering the form field:

    Error loading Partial View (file: ~/Views/MacroPartials/Global/Register.cshtml). Exception: System.InvalidOperationException: Impossibile trovare un url per la convalida remota. //Italian exception: means Impossible to find a url for remote convalidation
       in System.Web.Mvc.RemoteAttribute.GetUrl(ControllerContext controllerContext)
    
  • Alex Perotti 53 posts 94 karma points
    Mar 03, 2015 @ 17:57
    Alex Perotti
    0

    I'm trying to get out of this...

    public class RoutingHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarting(
                UmbracoApplicationBase umbracoApplication,
                ApplicationContext applicationContext)
            {
                RouteTable.Routes.MapRoute("EmailIsUnique",
                    "umbraco/surface/ValidationSurface/EmailIsUnique/",              
                    new
                    {
                        controller = "ValidationSurfaceController",
                        action = "EmailIsUnique"
                    });
    
            }        
        }
    

    this provide a strange behavior:

    without the route /umbraco/surface/ValidationSurface/EmailIsUnique/ calls the "EmailIsUnique" action.

    with the route the response is blank, but /umbraco/surface/ValidationSurface/EmailIsUnique/anythingIType calls the "EmailIsUnique" action.

    I really don't get why this is happening.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Mar 20, 2015 @ 02:42
    Nicholas Westby
    0

    Are you using Umbraco 7? If so, I wouldn't trust that documentation, which may be out of date. As I said, I believe surface controllers are now routed using a hidden field (that gets rendered when you call Html.BeginUmbracoForm). And surface controllers are auto-routed, so you don't need to map a route.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Nov 07, 2015 @ 23:00
    Nicholas Westby
    0

    For anybody following this, I explain surface controller routing in a bit more detail in this message: https://our.umbraco.org/forum/umbraco-pro/contour/60788-Umbraco-Forms-and-Ajax#comment-233528

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies