【问题标题】:How to create Web Application using C# and ASP.NET Core using macOS X如何使用 macOS X 使用 C# 和 ASP.NET Core 创建 Web 应用程序
【发布时间】:2022-09-30 22:05:45
【问题描述】:

我是 C# 和 ASP.NET Core 的初学者,我的任务是创建 Web 应用程序。 这个应用程序的主要任务是基本的:我需要创建一个表单(用户可以写东西的行)并显示书面内容。它还必须处理异常(FormatException) 当用户没有输入数字时

我开发的控制台应用程序的代码 sn-p,现在我需要借助 C# 和 ASP.NET Core 框架将其转换为 Web 应用程序。

这里是 C# 控制台应用程序的代码模板:

using System;
namespace Input_Output
{
   class MainClass
   {
     public static void Main(string[] args)
     {
        int number;
        try 
        {
           Console.WriteLine(\"Enter a number: \");
           number = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine($\"You\'ve entered {number}\");
        }
        catch(FormatException)
        {
          Console.WriteLine(\"It is not a number!!!\");
        }
        Console.ReadKey(true);
     }
   }
}

现在我需要将其转换为带有输入行的 Web 应用程序,如果它不是 10、49 等数值,则在特殊窗口中显示异常。我在 macOS X 上写这篇文章,所以不要为 Windows 提供建议:)

希望有人可以帮助我完成这项任务。谢谢 :) :) :)

    标签: c# asp.net web


    【解决方案1】:
        int number;
        try 
        {
           Console.WriteLine("Enter a number: ");
           var enteredNumber = Console.ReadLine();
           var isNumeric = int.TryParse(enteredNumber, out int n);
           if(isNumeric)
           {
             number = Convert.ToInt32(enteredNumber);
             Console.WriteLine($"You've entered {number} succesfully");
           }
           else
           {
             Console.WriteLine("It is not a number!!!");
           }
        }
        catch(Exception ex)
        {
          Console.WriteLine("An issue occured with your number entry: " + ex.message);
        }
        Console.ReadKey(true);
    

    【讨论】:

    • 我刚才做了一个更改,之前的代码在 TryParse 代码行附近是不正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多