About calling web api

李畋岷 76 Reputation points
2021-12-29T03:21:56.663+00:00

How to call web api, such as Window.alert and FileReader, in Razor components using c# instead of js?
Further, is it possible to thoroughly use c # instead of js in Blazor projects?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-12-29T09:15:01.303+00:00

    Hi @李畋岷 ,

    How to call web api, such as Window.alert and FileReader, in Razor components using c# instead of js?

    To the Window.Alert, if you want to show the alert, confirm or prompt dialog in Asp.net core Blazor application, you need to use the JSRuntime instance.

    Refer to the following sample code:

    161142-image.png

    The result is like this:

    161152-1.gif

    More detail information, refer to Call JavaScript functions from .NET methods in ASP.NET Core Blazor.

    For the FileReader, if you want to upload file in Asp.net core Blazor application, you can use the InputFile component to read browser file data into .NET code, see ASP.NET Core Blazor file uploads.

    Further, is it possible to thoroughly use c # instead of js in Blazor projects?

    Not sure which effect you want to achieve, but you can check the ASP.NET Core Blazor event handling, if using the event handling can achieve the same behavior, then there is no need to use JS.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-29T17:04:58.25+00:00

    Blazor code either runs on the server, or in the WASM sandbox depending on project type. This means it has no direct access to the browser dom, files, or network. JavaScript interop is required.

    You can code only in C#, if you can find blazor wrappers for the JavaScript interop you need to call. These would typically be 3rd party nuget packages.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.