Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1287 posts 2744 karma points
    Mar 28, 2013 @ 15:57
    Amir Khan
    0

    Render Node content from querystring

    Does anyone have an example of how to render a node's content based on a querystring in Razor? I can't find any documenetation for the life of me!

    -Amir

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2013 @ 06:16
    Tom Fulton
    0

    Hi Amir,

    Not sure what you mean - something like this?

    if (!string.IsNullOrEmpty(HttpContext.Current.QueryString["node-to-render"] == "1")) {
      var node = Umbraco.TypedContent(HttpContext.Current.QueryString["node-to-render"]);
      <p>@node.Name</p>
  • Curetos 7 posts 32 karma points
    Mar 29, 2013 @ 06:29
    Curetos
    0

    This is an example on using querystrings.

    It is Umbraco's github quickstart guide to using Examine.

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 15:49
    Amir Khan
    0

    Hi Tom, I get the following error when trying what you suggested.

    c:\inetpub\wwwroot\macroScripts\635004028828790000_RenderBlogQuery.cshtml(3): error CS1061: 'System.Web.HttpContext' does not contain a definition for 'QueryString' and no extension method 'QueryString' accepting a first argument of type 'System.Web.HttpContext' could be found (are you missing a using directive or an assembly reference?)

    Curetos, I keep getting nothing returned with the Razor querystring example in the link you shared.

    Any ideas?

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 16:18
    Charles Afford
    0

    Amir, UmbracoTypedContent(). takes a int as a parameter not HttpContext.

    You could parse the query string using Int32.TryParse(HttpContext.Current.QueryString["node-to-render"], out result)

    if(...... some logic)

    {

    }

    Why are you trying to get node content based on a query string, If you pass a nodeId on the query string could someone change it? Thus giving a diffrent result? Does not sound the most secure apporach :)

    Charlie :)

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 16:26
    Amir Khan
    0

    Hi Charlie, not sure I follow your suggestion exactly?

    The reason I'm rendering node content based on query string is to render content from a shared repository inside various templates for a multisite setup.

    Thanks!
    Amir

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 16:32
    Charles Afford
    0

    This is what you need:

    HttpContext.Current.Request.QueryString["node-to-render"]

    Another problem you have is the method you are trying to put the HttpContext into, i am sure only accepts int and maybe a list as a parameter.  Not your HttpContext. So if you want to do it you would need to parse the querystring into a int and then do what you will with it from there.  I suggested TryParse and you want to catch something that is not a int :).

    int nodeId = Int32.TryParse(HttpContext.Current.Request.QueryString["node-to-render"], out id);

    Does that make sense?

    Charlie :)

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 16:40
    Amir Khan
    0

    So something like this?

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @if (!string.IsNullOrEmpty(Request.QueryString["query"])) {
    var node = HttpContext.Current.Request.QueryString["query"];
    int post = Convert.ToInt32(node);
    <p>@post.Name</p>
    }


    That gives me this: 'int' does not contain a definition for 'Name


    Am I just completely misunderstanding this?

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 16:48
    Charles Afford
    0

    @inherits umbraco.MacroEngines.DynamicNode

    @string result= "";

    @if(!string.IsNullOrEmpty(Request.QueryString["query"]))

    {

     

    if(int32.TryParse(HttpContext.Current.Request.QueryString["query"], out result);

    DynamicNode node = new DynamicNode(result)

    node.Name

    }

    else

    {

    do something if the querystring was not parsed // not an int

    }

    Not tested but that should work i think :)

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 16:50
    Charles Afford
    0

    Is he site live btw?  What is the url :)

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 16:56
    Amir Khan
    0

    Hi Charles, the site is running locally currently. So I added the missing ) after "out result)" and now I get the following, is this nested if statement causing that?

    Expected a "{" but found a ";". Block statements must be enclosed in "{" and "}". You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed:

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 16:58
    Charles Afford
    0

    Yes it will be nesting :), sorry just pasted it into vs there were some errors one sec :D x

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 17:03
    Charles Afford
    101

    @{

    if(!string.IsNullOrEmpty(Request.QueryString["query"]))

    {

     int result= 0;

     if(Int32.TryParse(HttpContext.Current.Request.QueryString["query"].ToString(), out result))

     {

    umbraco.MacroEngines.DynamicNode node = new umbraco.MacroEngines.DynamicNode(result);

    <h1> @node.Name </h1>

     }

    else

    {

    do something if the querystring was not parsed // not an int

    }

    }

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 17:10
    Amir Khan
    0

    So I added the missing bracket and now I get this :)

    c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\e22c2559\92c7e946\App_Web_635004077497240000_renderblogquery.cshtml.70df5e80.ti7q3ryy.0.cs(42): error CS0115: 'ASP._Page_macroScripts_635004077497240000_RenderBlogQuery_cshtml.Execute()': no suitable method found to override

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 17:15
    Charles Afford
    0

    hmmmm, if you debug and step throught are you getting an ID from the query string?  Or can you render the out param and see the value?  :) 

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 17:17
    Charles Afford
    0

    thats an override problem.  You are trying to override a method a method that is not defined.  Dont think that should be my code

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 17:32
    Amir Khan
    0

    I'm not sure what was causing that. I manually retyped the code and it went away... Now I'm getting "Invalid expression term 'string'"!

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 17:34
    Charles Afford
    0

    Ok, well thats progress :).  Are you using my example above?  Have you got 'string' defined anywhere? :)

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 17:35
    Charles Afford
    0

    only one i can see is if(!string

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 17:40
    Amir Khan
    0

    Working great now Charlie! Thank you so much for your help!

  • Amir Khan 1287 posts 2744 karma points
    Apr 01, 2013 @ 17:42
    Amir Khan
    1

    Final working code incase anyone wants to copy / paste:

    @{
    if(!string.IsNullOrEmpty(Request.QueryString["query"]))
    {
    int result= 0;
    if(Int32.TryParse(HttpContext.Current.Request.QueryString["query"].ToString(), out result))
    {
    umbraco.MacroEngines.DynamicNode node = new umbraco.MacroEngines.DynamicNode(result);

    <h1> @node.Name </h1>
    }
    else
    {
    <h2>No query found</h2>
    }
    }

    }

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 01, 2013 @ 17:55
    Charles Afford
    0

    No problem :) glad you got it to work.  Still think you should and try and pass that Node information into your partial view from your own model rather than passing it on the query string.  But as long as it works :D.  Charlie

Please Sign in or register to post replies

Write your reply to:

Draft