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.
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 :)
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.
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);
@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
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:
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
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);
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
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
Hi Amir,
Not sure what you mean - something like this?
This is an example on using querystrings.
It is Umbraco's github quickstart guide to using Examine.
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?
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 :)
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
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 :)
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?
@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 :)
Is he site live btw? What is the url :)
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:
Yes it will be nesting :), sorry just pasted it into vs there were some errors one sec :D x
@{
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
}
}
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
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? :)
thats an override problem. You are trying to override a method a method that is not defined. Dont think that should be my code
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'"!
Ok, well thats progress :). Are you using my example above? Have you got 'string' defined anywhere? :)
only one i can see is if(!string
Working great now Charlie! Thank you so much for your help!
Final working code incase anyone wants to copy / paste:
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
is working on a reply...