Copied to clipboard

Flag this post as spam?

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


  • Joseph 14 posts 93 karma points
    Oct 01, 2021 @ 19:49
    Joseph
    0

    URL is not reaching controller

    I created a controller called UserManagerController.cs. I put it in the folder with our other controllers, including one we call AjaxController.

    I cannot reach user manager controller. When I enter the URL /umbraco/api/userManager/whatever I get a 404.

    I know this should work, because when I do /umbraco/api/ajax/whatever it works.

    The User Manager Controller is inheriting from the UmbracoApiController.

    What else can I check? Is there more I need to do to set up a new controller?

  • Thomas Kassos 54 posts 265 karma points
    Oct 01, 2021 @ 20:11
    Thomas Kassos
    0

    Could you post more details? some code sample both ajax and c#?

    Just to be more clear what you are trying to achieve.

    Just in case you if are trying to make a "post" you should decorate your endpoint with [HttpPost] as the HttpGet is the default method

  • Joseph 14 posts 93 karma points
    Oct 01, 2021 @ 20:24
    Joseph
    0

    Here is the JavaScript to call the controller in my view:

    <script>
    $('#newUser').submit(function (e) {
        e.preventDefault();
        var user = $("#user").val();
        var password = $("password").val();
        $.get("/umbraco/api/userManager/createUser/" + user + "/" + password);
    });
    

    And here is the method in my controller:

    [HttpGet()]
        [Route("createUser/{user}/{password}")]
        public void createUser(string user, string password)
        {
            Helper.GetUserDetails(user);
        }
    

    This is just to get it working, once it is working it will be totally different.

  • Thomas Kassos 54 posts 265 karma points
    Oct 01, 2021 @ 22:18
    Thomas Kassos
    0

    Try these 4 things.

    1 . Because of your

    [Route("createUser/{user}/{password}")]
    

    I think the url should be like that but I may be wrong.

    $.get("/umbraco/api/createUser/" + user + "/" + password);
    

    2 . Try add the full Url in your get method just in case...

    $.get("{yourDomain}/umbraco/api/userManager/createUser/" + user + "/" + password);
    

    3 .add [FromUri] to parameters

    public void createUser([FromUri]string user, [FromUri]string password)
    

    4 . remove the [Route] and do that to ajax

    $.get("{yourDomain}/umbraco/api/userManager/createUser?user=" + user + "&password=" + password);
    

    and your controller should look like that

    [HttpGet]
    public void createUser([FromQuery]string user, [FromQuery]string password)
    

    Eventually I believe you need to do an HttpPost as you want to create an account

    In that case your controller should look like that

    [HttpPost]
    public void CreateUser([FromBody]RegisterModel model)
    

    and your ajax should look like something that

    $.ajax({
        url: '{yourDomain}/umbraco/api/userManager/createUser,
        type: 'POST',
        data: ({ User: user, Password: password }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        async: false,
        cache: false,
        success: function (result) {
    
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
      }
    });
    
Please Sign in or register to post replies

Write your reply to:

Draft