locked
ASP.NET core razor pages c# equivalent to php echo and die RRS feed

  • Question

  • User-1209166642 posted

    In a model behind I want to see if a variable has what it is supposed to have.

    In php I can:

    echo $myvariable;
    die;

    In the model behind I tried:

    Response.Write(somevar);
    Response.End();

    And got:

    Severity Code Description Project File Line Suppression State
    Error CS1061 'HttpResponse' does not contain a definition for 'Write' and no accessible extension method 'Write' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?) pcore31 C:\Users\kpskl_000\source\repos\pcore31\pcore31\Pages\petnew\Create.cshtml.cs 58 Active


    Severity Code Description Project File Line Suppression State
    Error CS1061 'HttpResponse' does not contain a definition for 'End' and no accessible extension method 'End' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?) pcore31 C:\Users\kpskl_000\source\repos\pcore31\pcore31\Pages\petnew\Create.cshtml.cs 59 Active

    Okay, in a .net core project, how do I echo then die? 

    Thursday, April 30, 2020 4:54 PM

Answers

  • User475983607 posted

    The Razor syntax to write content is simply.

    @somevar

    Probably the closest to die is "return" or maybe "break" which does not make sense in Razor code.

    It seems you are trying to view variable values.  ASP.NET core uses logging.  The following write to the Output window in visual Studio if you running from VS.  Otherwise, it write to the console if you are hosting the application in kestrel by doing  "dotnet run" or switching the project from IIS Express to the Project name in the standard tool bar. 

        public class IndexModel : PageModel
        {
            private readonly ILogger<IndexModel> _logger;
    
            public IndexModel(ILogger<IndexModel> logger)
            {
                _logger = logger;
            }
    
            public void OnGet()
            {
                string somevar = "Hello World";
                _logger.LogInformation($"The variable is {somevar}");
            }
        }

    You'll probably be interested in reading the docs.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2020 5:24 PM
  • User-1209166642 posted

    Solved with:

    await Response.WriteAsync("no image");

    And very next line a break point.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 1, 2020 4:29 PM

All replies

  • User475983607 posted

    The Razor syntax to write content is simply.

    @somevar

    Probably the closest to die is "return" or maybe "break" which does not make sense in Razor code.

    It seems you are trying to view variable values.  ASP.NET core uses logging.  The following write to the Output window in visual Studio if you running from VS.  Otherwise, it write to the console if you are hosting the application in kestrel by doing  "dotnet run" or switching the project from IIS Express to the Project name in the standard tool bar. 

        public class IndexModel : PageModel
        {
            private readonly ILogger<IndexModel> _logger;
    
            public IndexModel(ILogger<IndexModel> logger)
            {
                _logger = logger;
            }
    
            public void OnGet()
            {
                string somevar = "Hello World";
                _logger.LogInformation($"The variable is {somevar}");
            }
        }

    You'll probably be interested in reading the docs.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2020 5:24 PM
  • User-1209166642 posted

    Solved with:

    await Response.WriteAsync("no image");

    And very next line a break point.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 1, 2020 4:29 PM