Copied to clipboard

Flag this post as spam?

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


  • Paul 2 posts 82 karma points
    Nov 07, 2023 @ 10:49
    Paul
    0

    Query string to node id

    Hi,

    I am using the following code to get a query string from the address bar..

    var eventid = HttpContextAccessor.HttpContext.Request.Query["event"];
    

    I am then trying to use the result to get page content....

    var page = UmbracoContext.Content.GetById(eventid);
    

    But it seems as though it doesn't like the fact that eventid is a string and I cannot seem to convert the string to an integer whatever I try.

    Any ideas would be greatly appreciated.

    I am using Umbraco 12

    Thanks

  • Nemanja Stankovic 9 posts 99 karma points
    Nov 07, 2023 @ 11:42
    Nemanja Stankovic
    100

    Hi @Paul,

    You can do the following:

    // if you want to have int as an output

    int eventid = int.Parse(HttpContextAccessor.HttpContext.Request.Query["event"].ToString());

    // output comes back as string

    var eventid = HttpContextAccessor.HttpContext.Request.Query["event"].ToString();

    // Umbraco.Content() takes both string and int

    IPublishedContent page = Umbraco.Content(eventid);

    Hope this helps.

    Cheers

  • Paul 2 posts 82 karma points
    Nov 07, 2023 @ 11:56
    Paul
    0

    Fantastic. Worked perfectly.

    Thankyou

Please Sign in or register to post replies

Write your reply to:

Draft