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

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

Related

.NET Core Blazor Dynamically retreive html as string from Razor Page or Blazor Component

I have a Razor Page (.cshtml) which is a PDF Template. Now i am required to retreive html as string from the page itself to Save it as a PDF file. In MVC, i could use IRazonEngine interface for the same. But in Blazor what can used to obtain this.
Additionally, if i can use Blazor component instead of a Razor Page, how can i get the string of HTML?
Please help!
This is the sample Razor Page I have
#model UserModel
<div>
<strong>Full Name:</strong>
<div>#Html.DisplayFor(model => model.FullName)</div>
</div>
<div>
<strong>Address:</strong>
<div>#Html.DisplayFor(model => model.Address);</div>
</div>
<div>
<strong>DOB:</strong>
<div>#Html.DisplayFor(model => model.DateOfBirth);</div>
</div>
UPDATE :
I have now used RazorLight.NETCore3 and working smooth.
I think this is not about blazor or mvc pages, but you can use RazorEngine.NetCore library to convert the cshtml and fill in the dynamic data using a model and retrieve the completed html.
Here is a guide on how to do it: https://khalidabuhakmeh.com/generate-outputs-with-razor-engine-in-dotnet-core
Github project: https://github.com/fouadmess/RazorEngine
Here is a simple code which I have used in the past:
public string CompileContent(string content, object model)
{
var stringContent = Engine.Razor.RunCompile(content, Guid.NewGuid().ToString(), null, model);
return stringContent;
}

How can I preview live(on browser) with the hbs format

I use the editor intellij Idea how can I debug an hbs file in the browser, the problem is that the file is displayed but it only shows the code on the page, it does not display the content itself
Handlebars can't be directly executed by the browser, you have to compile them first, like
<!-- Include Handlebars from a CDN -->
<script src="https://cdn.jsdelivr.net/npm/handlebars#latest/dist/handlebars.js"></script>
<script>
// compile the template
var template = Handlebars.compile("Handlebars <b>{{doesWhat}}</b>");
// execute the compiled template and print the output to the console
console.log(template({ doesWhat: "rocks!" }));
</script>
See https://handlebarsjs.com/installation/#installation

#Ajax.ActionLink in ASP.NET CORE [duplicate]

Earlier in MVC I used #Ajax.ActionLink for Ajax call and replaced container in my layout.
Now in .Net Core there is anything like AjaxHelper back then.
How can I form Ajax call without writing jquery script for every menu item in my dashboard.
I tried #Url.Action with anonymous Ajax parameters but that won't work.
#Url.Action("Index", "User", new
{
data_ajax = "true",
data_ajax_method = "GET",
data_ajax_mode = "replace",
data_ajax_update = "#replace"
}))"
No. Honestly, you never needed it anyways. All it did was create a regular link and then add a trivial bit of JavaScript you can easily add yourself. Long and short, let it go:
<a class="menu-item" asp-action="Index" asp-controller="User">Click Me</a>
Then:
<script>
$('.menu-item').on('click', function (e) {
e.preventDefault();
$.get(this.href, function (html) {
$('#replace').html(html);
});
});
</script>
By binding to the class, you only need this one piece of JS for any link with a menu-item class.

Microsoft WebHelpers with NETCore.App (2.1)

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

How to integrate visual captcha in jsf page with javascript of jquery

I am trying to integrate visual captcha in my jsf page. is there a link where i can see steps for doing it. i followed the below steps from visual captcha for jquery but it didn't work.
Initialization
Include jQuery library and jQuery version of the visualCaptcha front-end library into the HTML page:
Create a visualCaptcha container into the HTML page:
Initialize the captcha with the $( element ).visualCaptcha( options ) jQuery function that returns a jQuery object within the visualCaptcha object. It uses two parameters: element is a selector of a container for the visualCaptcha, options is an object of the visualCaptcha options:
var el = $( '#sample-captcha' ).visualCaptcha( {
imgPath: 'img/',
captcha: { numberOfImages: 5 }
} );
// use the following code to get the captcha object
var capthca = el.data( 'captcha' );