Microsoft WebHelpers with NETCore.App (2.1) - asp.net-core

I'm trying to get the below code to work, but I keep getting compatibility problems with Microsoft.Web.Helpers v 3.2.6 and my current SDK package of NETCore 2.1. Also, for the life of me, I can't get the simplest calls of IsPost and Request to be recognized. I'm sure it's an obvious fix, but I can't find it!
Thanks in Advance for any direction...
#using Microsoft.Web.Helpers;
#{
var fileName = "";
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" +
fileName);
uploadedFile.SaveAs(fileSavePath);
}
}
<!DOCTYPE html>
<html>
<head>
<title>FileUpload - Single-File Example</title>
</head>
<body>
<h1>FileUpload - Single-File Example</h1>
#FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag:true,
uploadText:"Upload")
#if (IsPost) {
<span>File uploaded!</span><br/>
}
</body>
</html>

The WebHelpers library is not compatible with ASP.NET Core. It relies on System.Web, which .NET Core has been designed to move away from.
The replacement for the IsPost block is a handler method. By convention, a handler method named OnPost will be executed if the method used to request the page is POST (which is what the IsPost property used to check).
Personally, I never understood the point of the FileUpload helper unless you wanted to allow the user to add additional file uploads to the page (which you clearly don't in this case). An input type="file" is easier to add to a page.
File uploading in ASP.NET Core is completely different to Web Pages. Here's some guidance on it: https://www.learnrazorpages.com/razor-pages/forms/file-upload

Related

How to add different CSS files to different layouts in blazor?

Hi every blazor lover!
I have 2 different layouts and, I want to load different CSS file on each layout.
The first one is MainLayout.razor and the other is AdminLayout.razor.
I want to load my admin menu CSS file in the AdminLayout, without using "css isolation", because the CSS files for this layout maybe more files in future.
ASP.NET Core 3.1 or .NET 5.0
Thanks in advance.
You can use the HTML <link> tag anywhere in the <head> or <body> to include CSS, so drop the appropriate <link rel="stylesheet" href="..." /> into MainLayout.razor and AdminLayout.razor, respectively.
Eventually, adding content to the <head> directly from a razor component may be supported, as tracked here.
Solution for .Net 3.1 or .Net 5.0
use this java script to add css file to page or layout directly:
function loadCss(cssPath) {
var ss = document.styleSheets;
for (var i = 0, max = ss.length; i < max; i++) {
if (ss[i].href.includes(cssPath.substr(2)))
return;
}
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = cssPath;
document.getElementsByTagName("head")[0].appendChild(link);
}
I create a js function "loadCss", you have to call this function on OnAfterRenderAsync event:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await JsRuntime.InvokeVoidAsync("loadCss", "css/FILENAME.min.css");
}

Howto debug JavaScript inside ASP .Net Core 3.1 MVC applications (Razor view - *.cshtml)?

I have latest Visual Studio 2019 16.5.4 Enterprise.
I've just created an ASP .Net Core 3.1 MVC application from a template (with default settings).
And I've added some JavaScript code to a Home Page's Index.cshtml:
#{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about building Web apps with ASP.NET Core.</p>
</div>
#section Scripts {
<script>
function GetJSON_Simple() {
var resp = [];
return resp;
}
debugger;
var simpleData = GetJSON_Simple();
</script>
}
And I'm not able to debug JavaScript code (breakpoints inside GetJSON_Simple function body or on var simpleData = GetJSON_Simple() is never hit). I've tried both Chrome and MS Edge (Chromium).
According to this article (Debug JavaScript in dynamic files using Razor (ASP.NET) section):
Place the debugger; statement where you want to break: This causes the
dynamic script to stop execution and start debugging immediately while
it is being created.
P.S. I've already have Tools->Options->Debugging->General with turned on Enable JavaScript Debugging for ASP.NET (Chrome and IE) checkbox and of course I'm compiling in Debug.
My test project is attached
Howto debug JavaScript inside ASP .Net Core 3.1 MVC applications
(Razor view - *.cshtml)?
In fact, it is already a well-known issue. We can not debug the js code under Net Core razor page but only for code in separate js or ts files. See this link.
Solution
To solve it, I suggest you could create a new single js file called test.js and then reference it in razor page and then you can hit into js code when you debug it.
1) create a file called test.js and migrate your js code into it:
function GetJSON_Simple() {
var resp = [];
return resp;
}
debugger;
var simpleData = GetJSON_Simple();
2) change to use this in your cshtml file:
#{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about building Web apps with ASP.NET Core.</p>
</div>
#section Scripts {
<script scr="xxx/test.js">
</script>
}
3) clean your project, then rebuild your project and debug it and you will hit into Js code.
Another way is to explicitly define the source mapping name for the script directly in your javascript code via a sourceURL comment.
<script>
...your script code
//# sourceURL=blah
</script>
The script in that block will show up in Chrome with the value you specified. You can then view and debug just like a normal .js file.
This is especially useful if you don't want to refactor an existing codebase that has embedded javascript in the cshtml files or really ANY codebase that has javascript loaded on the fly.
Live Example
You can actually see an example with the built-in javascript runner here. Just click 'Run Snippet' and then search for "blahblah" in your Page sources. You should see this:
alert("test");
//# sourceURL=blahblah

Opening HDevelop examples from internal function description

We have an extensive internal Halcon library, which is used by more and more people. Now we have arrived at the point, where it would be very useful if we could directly access HDevelop examples written specifically for this function as it is already available for MVTec's functions.
This is not possible at the moment, and MVTec will not provide this in the future (their words).
I have made some progress but I'm not able to find a solution.
If a text is filled in one of the boxes in Procedures/Edit Interface/General Documentation/Suggestions it generates an HTML link. Is is possible to start HDevelop using Internet Explorer:
<html>
<head>
<script type="text/javascript">
function foo() {
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("hdevelop.exe");
var input = "";
while (!oExec.StdOut.AtEndOfStream) {
input += oExec.StdOut.ReadLine() + "<br />";
}
if (input)
document.getElementById("dummy").innerHTML = input;
}
</script>
</head>
<body onload="foo();">
<code id="dummy"></code>
</body>
</html>
Unfortunately if the reference to an HTML document is local, it is opened inside the HDevelop's browser, which does not support ActiveX objects. If the reference starts with http, it is opened in your default browser.
Does anyone else have another solution which wouldn't require local server to access local files and setting your default browser to IE?
It is possible to do it by generating a simple HTML file:
<html>
<body>
<TT>Example</TT>
</body>
</html>
and referencing it in any of the Suggestions fields without the .html extension:
There is going to be one step more when accessing the example, where you have to click on the hyperlink "Example" because automatic redirecting is not working.

Sample Code for Google Translate API

I want to use the Google Translate API in eclipse and I've checked the Google API website, but there isn't any sample code for the Google Translate API in Java. Can someone provide some and explain how to get started. I have already read the documentation on this API.
https://cloud.google.com/translate/v2/quickstart#JSONP
<html>
<head>
<title>Translate API Example</title>
</head>
<body>
<div id="sourceText">Hello world</div>
<div id="translation"></div>
<script>
function translateText(response) {
document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;
}
</script>
<script>
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
var sourceText = escape(document.getElementById("sourceText").innerHTML);
// WARNING: Your API key will be visible in the page source.
// To prevent misuse, restrict your key to designated domains or use a
// proxy to hide your key.
var source = 'https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&source=en&target=de&callback=translateText&q=' + sourceText;
newScript.src = source;
document.getElementsByTagName('head')[0].appendChild(newScript);
</script>
</body>
</html>
The Google Client Libraries are now the recommended APIs to use. You can now find Java specific Translation API Client Library examples in the documentation.
To report any issues with the new Java specific Client Libraries, you can directly file an issue on the GitHub Issue tracker.
you can installe this package for laravel
https://packagist.org/packages/charef/free-translate-api

ABCpdf doesn't render images in an web application under IIS6

I'm trying to render a web page that contains images into a pdf document using ABCpdf. This is done from a web application.
When I run the application on my development machine in IIS5, everything is fine. When I deploy the application on IIS6, the images don't appear in the pdf.
To reproduce the problem, I made a simple web application to render a pdf file from a simple web page and I found out that the images which are not local are the ones that don't appear in the pdf.
The relevant code that interacts with ABCpdf is:
Doc theDoc = new Doc();
theDoc.Rect.Inset(18, 18);
theDoc.HtmlOptions.PageCacheEnabled = false;
theDoc.HtmlOptions.PageCacheClear();
theDoc.HtmlOptions.UseNoCache = true;
theDoc.HtmlOptions.Timeout = 60000;
int theID = theDoc.AddImageUrl(theUrl);
while (true)
{
if (!theDoc.Chainable(theID)) break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
for (int i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;
theDoc.Flatten();
}
theDoc.Save(location);
theDoc.Clear();
The html page that I'm using for test is this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test page</title></head>
<body>
<p>This is a local image</p>
<img src="http://myserver/test/images/testimage.gif" />
<p>This is a remote image</p>
<img src="http://l.yimg.com/a/i/ww/beta/y3.gif" />
</body>
</html>
So I'm trying to render the page at this url: http://myserver/test/testpage.html (the code above) into a pdf.
In IIS6, the second image (that is not local for the server) doesn't appear in the pdf.
It seems to be a problem with access rights, but I couldn't figure it out.
Thank you.
I know this is a little late, but hopefully will help someone else!
Just been experiencing a very similar problem (which is how I landed at this page..). The version of IIS was the same, but it was being run on a different server. Looks like the problem was more generation of the PDF before the image has finished downloading.
I got in touch with WebSuperGoo. The said under the hood it uses MSHTML (good chance that's the difference in your environments) and a couple of suggestions were to try:
theDoc.SetInfo(0, "CheckBgImages", "1");
and
theDoc.SetInfo(0, "RenderDelay", "5000"); // You can change this value, just an initial test.
The second will delay rendering the PDF, giving the image a chance to download.
I had a similar issue and found it was caused by the size of the image file being too large.