如何在 ASP.Net Web API 上获取日期和时间?
How to get date and time on ASP.Net Web API?
我是 ASP.Net Web API 的新手,想开发一个示例来获取日期时间。
我开发了两个 applications.In 第一个我有我的 API 和 运行 它抛出 Visual Studio 另一个是控制台应用程序来测试第一个。
在我的 API 我有:
public class DateTimeController : ApiController
{
public DateTime Get()
{
return DateTime.Now;
}
}
在我的控制台应用程序上我有这个,但我不知道它是否正确,因为它不起作用:
static void Main(string[] args)
{
string baseAddress = "http://localhost:13204/api/DateTime/get";
using (HttpClient httpClient = new HttpClient())
{
Task<String> response =
httpClient.GetStringAsync(baseAddress);
}
Console.ReadLine();
}
快速查看响应:
response.Status=等待激活
response.id=4
response.AsyncState=空
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DateTime", action = "Get", id = UrlParameter.Optional }
);
}
}
这里有几个问题。
HttpClient.GetStringAsync returns a Task<string>
but you are not even assigning the results to a variable (or at least you weren't when you first posted the question). With methods that return a Task, you need to await them or wait on them until the task is finished. A lot of methods for doing that in a console app are described here。我将选择一个并向您展示如何实现它。
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
System.Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};
MainAsync(args, cts.Token).Wait();
}
static async Task MainAsync(string[] args, CancellationToken token)
{
string baseAddress = "http://localhost:13204/api/DateTime";
using (var httpClient = new HttpClient())
{
string response = await httpClient.GetStringAsync(baseAddress);
}
}
response
变量将是一个字符串,其中包含包裹在 JSON 中的日期时间。然后你可以使用你喜欢的JSON解析库(我喜欢Json.NET)来获取值。
请注意,没有必要在 URL 中指定特定的操作方法,因为我们发送了一个 HTTP GET 请求,并且由于该操作方法以 "Get" 开头,因此框架很智能足以知道它应该映射到该操作方法。
我是 ASP.Net Web API 的新手,想开发一个示例来获取日期时间。 我开发了两个 applications.In 第一个我有我的 API 和 运行 它抛出 Visual Studio 另一个是控制台应用程序来测试第一个。
在我的 API 我有:
public class DateTimeController : ApiController
{
public DateTime Get()
{
return DateTime.Now;
}
}
在我的控制台应用程序上我有这个,但我不知道它是否正确,因为它不起作用:
static void Main(string[] args)
{
string baseAddress = "http://localhost:13204/api/DateTime/get";
using (HttpClient httpClient = new HttpClient())
{
Task<String> response =
httpClient.GetStringAsync(baseAddress);
}
Console.ReadLine();
}
快速查看响应:
response.Status=等待激活
response.id=4
response.AsyncState=空
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DateTime", action = "Get", id = UrlParameter.Optional }
);
}
}
这里有几个问题。
HttpClient.GetStringAsync returns a Task<string>
but you are not even assigning the results to a variable (or at least you weren't when you first posted the question). With methods that return a Task, you need to await them or wait on them until the task is finished. A lot of methods for doing that in a console app are described here。我将选择一个并向您展示如何实现它。
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
System.Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};
MainAsync(args, cts.Token).Wait();
}
static async Task MainAsync(string[] args, CancellationToken token)
{
string baseAddress = "http://localhost:13204/api/DateTime";
using (var httpClient = new HttpClient())
{
string response = await httpClient.GetStringAsync(baseAddress);
}
}
response
变量将是一个字符串,其中包含包裹在 JSON 中的日期时间。然后你可以使用你喜欢的JSON解析库(我喜欢Json.NET)来获取值。
请注意,没有必要在 URL 中指定特定的操作方法,因为我们发送了一个 HTTP GET 请求,并且由于该操作方法以 "Get" 开头,因此框架很智能足以知道它应该映射到该操作方法。