Using Razor.Engine.NetCore in rest api solution .net core 3.1 with Url.Encode - razor

I'm trying to use Razor.Engine outside of MVC, everything works good, but now I need to encode url inside Razor template.
My template is:
<html>
<p><span>Click here</span></p>
</html>
My Model is:
BaseUrl = "http://mysiteurl",
UrlParams = "{"id":"43869906-dfdb-435c-a570-c4578d859e78"}"
I need to encode the UrlParams so I tried
href="#Model.BaseUrl?data=#Url.Encode(#Model.UrlParams)"
But no luck :(
Is it possible to encode url inside Razor template? and how?
I could not find the ability to add namespaces
I Also tried with RazorEngineService.create(config).RunCompile(...) but still not working
Thanks

So I ended up with this
#Model.BaseUrl?data=#System.Net.WebUtility.UrlEncode(#Model.UrlParams.ToString())
and it's working :)

Related

Using Angular to get html of a website URL

I am new in Angular
What I am going to try is to get the HTML of a page and reproduce it into an iFrame (it is an exercise).
I am using the following piece of code:
var prova = this._http.get(myUrl, {responseType: "text"}).subscribe((x) =>{
console.log(x);
});
I did it on a website (if is needed I can also insert the name of the pages) and it returns the html only of some pages.
In the other case the string x is empty.
Could it depend on connection?
Or there is some way to wait the end of the get request?
Or simply is wrong my approach and I should make a different type of request?
Your most likely going to need to use a library like puppeteer if you want to render a page properly. Puppeteer is a node library and useless headless chrome so I am not sure how well you could really integrate with Angular.
https://github.com/GoogleChrome/puppeteer

How to parse a Razor template with partials from a custom folder?

I have two cshtml-files in the same subfolder of Views. One of the templates is meant to include the other template. I tried to accomplish that as follows:
Main template:
<html>
<head></head>
<body>
#Html.Partial("~/Views/Pdfs/Header");
</body>
</html>
The error I get is
Unable to compile template. The name 'Html' does not exist in current context.
What am I supposed to do additionally?
As commented by Erik there is no Html in RazorEngine (see the linked answer), however you can use #Include("mytemplate") instead.
If you want to be compatible with the #Html.Partial() syntax for some reason you can extend the RazorEngine syntax like this.
Basically what you want to do is provide your own class inheriting from TemplateBase<T> (or ITemplate to be exact) and then set it either via configuration or the #Inherit MyBaseClass<MyModel> syntax. In this case you could just call the Include method from your Partial method within the Html helper class.
Been annoyed by this for a long time. Wrote all the infrastructure classes to just get this working like you'd expect in MVC, without all the MVC burden:
var razor = RazorHelper.O;
var html = razor.RenderFromMvc(#"Views\RazorEngine\TestEmail.cshtml", vm);
https://github.com/b9chris/RazorEngineComplete

Update HTML title to contain access code

I am trying to set up OAuth2.0 for an iPhone app I'm working on.
I have "http://www.mywebsite.com/success" set up as my RedirectURI to which the service I am working with appends a response code and state. My response becomes "http://www.mywebsite.com/success?code=ftlZcvFZ3RACFqzgxHypJw637jObmAoHowSuyxeM&state=".
The example I am following has me trying to access this code by the following:
[webView stringByEvaluatingJavaScriptFromString:#"document.title"];
Right now my page title is static and is always just Success, while the example expects it to include the code like: "Success ftlZcvFZ3RACFqzgxHypJw637jObmAoHowSuyxeM".
How do I modify my html to have the title reflect this?
Thanks!
You can use the sever-side language PHP. It's easy to learn.

what did richfaces do to javascript JSON?

I want to use JSON.stringify() to convert my JS object to a json string. But when I use richfaces components in the web page, I got the wrong result. See the example below. Please help me. Thank you!
this is the codes:
function testJSON()
{
var selArray = new Array();
selArray.push("aaa");
selArray.push("bbb");
selArray.push("ccc");
alert(JSON.stringify(selArray));
}
I got this if there is no richfaces components in the page.
["aaa","bbb","ccc"]
And if there is any richfaces component like "rich:modalpanel" in the page, it will get the following wrong JSON string.
"[\"aaa\",\"bbb\",\"ccc\"]"
I think that's because richfaces has done songthing that affected JSON.
So what can I do to avoid the effect of richfaces ?
Thank you very much!
Perhaps you are running into this issue, since Richfaces uses Prototype: JSON.stringify() array bizarreness with Prototype.js
I was able to fix the problem with one of the answers: https://stackoverflow.com/a/6611126/132374

JSON output in Umbraco 5

I just switched from Umbraco 4 to Umbraco 5 and it seems like a lot has changed.
So what i basically want is a possibility to add a alternative template to my document-types.
The fall back template shall return the content as JSON.
Why, you say? I want to create an API-like way of accessing the Umbraco data from my mobile devices.
WebAPI (http://cultiv.nl/blog/2012/4/22/exposing-umbraco-5-content-through-the-aspnet-web-api/)
I have thought about using WebAPI from asp.net MVC 4, but the project is really just a proof of concept and i don't want to code each endpoint.
So i found a som guys that did a package for Umbraco 4 that actually does this and renders the content of #currentPage as Json. The template is hit by adding "/JSON" to the end of the url. Unfortunetly this uses xslt, which ihas been removed from Ubraco 5.1 (Good thing).
So. I bet it's simple to create a partial, a macro or a partial macro that does this and add it to a template. But is just cant figure out where to start.
Any help with that? What I'm looking for is a step guide on what steps to take, to make the setup. Rendering the stuff ad json in C# i can handle.
It's the integration into Umbraci I lack.
Hoe u can help.
The alternative templating works exactly like in v4: Just add the name of the template at the end of the url: yoururl/json
Then add a new razor view to the templates named "json" with the following code:
#inherits RenderViewPage
#using System.Web.Mvc.Html;
#using Umbraco.Cms.Web;
#{
Layout = "";
}
{
#foreach (var attribute in Model.Attributes)
{
string.Format("\"{0}\": \"{1}\"", attribute.AttributeDefinition.Alias, attribute.DynamicValue);
}
}
This code can be used as a starting point without using the web api or own controllers.
Don't forget to allow the tempplate on all document types
hth,
Thomas