Umbraco Controller Route Error
# help-with-umbraco
j
I have a controller inherited from ControllerBase. My controller route is "users". I have a method HttpGet("{email}"), I got a server error. Based on .Net documentation this should work. Now I have to do "{email}/" or "something/{email}" or "{email}/something" to make it work. Is this a bug? https://cdn.discordapp.com/attachments/1220403261584965864/1220403261765582958/MicrosoftTeams-image.png?ex=660ed01b&is=65fc5b1b&hm=edca9d02dd58ccc71473c5361b9fc5f361bf2d041c442d9f6924f0df32915466&
i
Can you show the code from your controller method?
j
Copy code
[Route("wcs/users")]
[ApiController]
public class WCSUsersController : ControllerBase
{
    [HttpGet]
    public ActionResult Users_Get(int pageNumber, int perPage){}

    [HttpGet("{email}")]
    public ActionResult User_Get(string email){}
}
Here is the code, but User_Get returned 500.
s
It works, but as you can see in the error, you can't get an
UmbracoContext
. This is because
UmbracoContext
usually only exists in a http request context.
You should probably be able to do something like I've done here with the
backgroundScope
- https://cultiv.nl/blog/using-hangfire-to-update-umbraco-content/
j
but Route "{email}/" works? I didn't use UmbracoContext.
s
Not sure then.. what does the code in the
Users_Get(int pageNumber, int perPage)
method look like?
j
Use IMemberService get all members
s
That might need an
UmbracoContext
depending on what your doing. Comment out all of the code in that method to see if the route works, if it does, you'll probably need to ensure a context as linked aboved
j
Commented all the code just return Ok(). Still returned 500
s
What does the
detail
say?
j
Copy code
{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
  "title": "System.InvalidOperationException",
  "status": 500,
  "detail": "Wasn't able to get an UmbracoContext",
  "traceId": "00-81dff01760e4d72a7750b50f1019d1a9-be63e205d11557b4-00",
  "exception": {
    "ValueKind": 1
  }
}
same response.
I'm fine with "{email}/", but just want you guys aware that "{email}" won't work, looks like it's a bug
s
what is the url you're trying out that is NOT working? and what is the URL that is working fine?
j
"wcs/users/{email}" not working, "wcs/users/{email}/" is working
s
This is my test code:
Copy code
csharp
[Route("wcs/users")]
[ApiController]
public class WCSUsersController : ControllerBase
{
    [HttpGet]
    public ActionResult Users_Get(int pageNumber, int perPage)
    {
        return Ok();
    }

    [HttpGet("{email}")]
    public ActionResult User_Get(string email)
    {
        JsonResult result = new JsonResult(new { email = email });
        return Ok(result);
    }
}
and this is the result, seems to work? https://cdn.discordapp.com/attachments/1220403261584965864/1220735416525656175/image.png?ex=66100573&is=65fd9073&hm=c044ba469fbb5f11d274b17e6cdc01429aaa8f8b0da833511cfb9b881623d9e3&
And updated version to actually get the member works too:
Copy code
csharp
[Route("wcs/users")]
[ApiController]
public class WCSUsersController : ControllerBase
{
    private readonly IMemberService _memberService;

    public WCSUsersController(IMemberService memberService)
    {
        _memberService = memberService;
    }

    [HttpGet]
    public ActionResult Users_Get(int pageNumber, int perPage)
    {
        return Ok();
    }

    [HttpGet("{email}")]
    public ActionResult User_Get(string email)
    {
        var member = _memberService.GetByEmail(email);
        JsonResult result;
        if (member != null)
        {
            result = new JsonResult(new { id = member.Id });    
        }
        else
        {
            result = new JsonResult(new { email = email });
        }

        return Ok(result);
    }
}
https://cdn.discordapp.com/attachments/1220403261584965864/1220736983442128916/image.png?ex=661006e9&is=65fd91e9&hm=6d1f65115beada9e7b8a1b2900acddb82507f32be919be48bc939e6700470a96&
j
I think I found the issue, the issue is from my userService, I injected my userService
public UserService( Entities.DB.WcsContext wcsContext, MessagingService messagingService, EncryptionService encryptionService, EmailService emailService, IMemberManager memberManager, IPublishedContentQuery publishedContentQuery, IMemberService memberService)
This is inside my UserService, I think IPublishedContentQuery cause the problem cannot find UmbracoContext
s
Try a background scope 😉 👆
j
Thanks, but still "{email}/" works doesn't makes sense lol...
I still think it's not makes sense.
wcs/users/{email} (User_Get action)
error on
_memberManager.AsPublishedMember(memberByEmail)
, but
wcs/users (User_Gets action)
did not error on
_memberManager.AsPublishedMember(memberByEmail)
, they both load custom properties from Umbraco.
3 Views