locked
why console isn't working in this code RRS feed

  • Question

  • User887623398 posted

    Hello

    I'm trying to make an online app to send images to Instagram for educational purposes.

    This is just a simple page but the console hasn't any activity.

    Please inform me if you can.

    Here is the code:

    @page "/"
    @using System;
    @using System.Collections.Generic;
    @using System.IO;
    @using System.Threading.Tasks;
    @using InstaSharper.API;
    @using InstaSharper.API.Builder;
    @using InstaSharper.Classes;
    @*@using InstaSharper.Examples.Samples;*@
    @using InstaSharper.Logger;
    @*@using Microsoft.AspNetCore.Components.Web;*@
    
    
    
    
    
    <div>
    
    <input type="text" id="uName" @bind="username" @bind:event="oninput" />
    
    </div>
    
    <div>
    
    <input type="text" id="pword" @bind="password" @bind:event="oninput" />
    
    </div>
    
    <div>
    
        <b>username: </b> @username
        
    </div>
    
    <div>
        
        <b>password:</b> @password
    
    </div>
    <div>
        <button type="button" @onclick="@OnEnter">enter</button>
    </div>
    <div>
        <h5>@Message</h5>
    </div>
    
    
        
    
    
    
    @code{
    
        public string username { get; set; } = string.Empty;
        public string password { get; set; } = string.Empty;
        public string Message { get; set; } = string.Empty;
    
    
    
    
    
        private static IInstaApi _instaApi;
    
        void OnEnter()
        {
    
            Console.WriteLine("ggg");
    
            Message = "ttt";
    
            var result =   Task.Run(MainAsync).GetAwaiter().GetResult();
            if (result)
                return;
    
    
    
    
        }
    
    
    
    
        public static  async Task<bool> MainAsync()
    
        {
            try
            {
    
    
    
                var userSession = new UserSessionData
                {
                    UserName = "username",
                    Password = "password"
    
    
    
                };
    
                var delay = RequestDelay.FromSeconds(2, 2);
                // create new InstaApi instance using Builder
                _instaApi = InstaApiBuilder.CreateBuilder()
                        .SetUser(userSession)
                        .UseLogger(new DebugLogger(LogLevel.Exceptions)) // use logger for requests and debug messages
                        .SetRequestDelay(delay)
                        .Build();
    
    
                Console.WriteLine("kkkk");
                //string Message = "ggg";
    
                return true;
    
    
    
            }
    
    
    
            catch (Exception e)
            {
                //string Message = e.Message;
                Console.WriteLine("hhh");
            }
    
            return false;
    
    
        }
    }
    

    thanks

    Saeed

    Tuesday, June 23, 2020 2:16 PM

All replies

  • User-474980206 posted

    Console.Write() only works with server side Blazor, and it writes to the server console (which is not available if hosted with IIS). run directly with dotnet. to write to the browser console:

    await jsRuntime.InvokeAsync<string>("console.log", "hello world");

    Tuesday, June 23, 2020 8:32 PM
  • User887623398 posted

    Thanks, Bruce. Should I add javascript or any namespace?

    Is there any way to alert in Blazor. like:

    await JSRuntime.InvokeVoidAsync("alert", "Hello world");

    Wednesday, June 24, 2020 3:35 PM
  • User-474980206 posted

    simple article on javascript interop

      https://chrissainty.com/using-javascript-interop-in-razor-components-and-blazor/

    Wednesday, June 24, 2020 3:49 PM
  • User887623398 posted

    Just javascript should be used?

    There isn't any other way? for example in C#?

    Wednesday, June 24, 2020 8:16 PM
  • User-821857111 posted

    Console.Write() only works with server side Blazor,
    Works fine for me in a client app - writes to the browser console.

    Monday, June 29, 2020 8:09 AM