我通常使用 PHP 和 Python 编写代码,但在这种情况下,我必须使用 C# 编写代码。
我有这个代码,它真的很好用。它是一个控制台应用程序。
但是你怎么能把它变成一个 C# .net 以便可以把它放在一个 IIS 上呢?
基本上不是将其输出到控制台,而是将其写入浏览器。
我试图搜索 C# Web,但找不到任何东西。
谢谢您的帮助!
using System;
using System.Net;
using Independentsoft.Exchange;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
NetworkCredential credential = new NetworkCredential("username", "password");
Service service = new Service("https://myserver/ews/Exchange.asmx", credential);
try
{
IsGreaterThanOrEqualTo restriction1 = new IsGreaterThanOrEqualTo(AppointmentPropertyPath.StartTime, DateTime.Today);
IsLessThanOrEqualTo restriction2 = new IsLessThanOrEqualTo(AppointmentPropertyPath.EndTime, DateTime.Today.AddDays(1));
And restriction3 = new And(restriction1, restriction2);
FindItemResponse response = service.FindItem(StandardFolder.Calendar, AppointmentPropertyPath.AllPropertyPaths, restriction3);
for (int i = 0; i < response.Items.Count; i++)
{
if (response.Items[i] is Appointment)
{
Appointment appointment = (Appointment)response.Items[i];
Console.WriteLine("Subject = " + appointment.Subject);
Console.WriteLine("StartTime = " + appointment.StartTime);
Console.WriteLine("EndTime = " + appointment.EndTime);
Console.WriteLine("Body Preview = " + appointment.BodyPlainText);
Console.WriteLine("----------------------------------------------------------------");
}
}
Console.Read();
}
catch (ServiceRequestException ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.WriteLine("Error: " + ex.XmlMessage);
Console.Read();
}
catch (WebException ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.Read();
}
}
}
}
编辑:我试图使它成为一个 asp.net 页面,但它不会在屏幕上打印任何内容。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Plan.NBT.Final.Default" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="Independentsoft.Exchange" %>
<%
NetworkCredential credential = new NetworkCredential("tedy", "123456889");
Service service = new Service("https://area51.com/EWS/exchange.asmx", credential);
try
{
IsGreaterThanOrEqualTo restriction1 = new IsGreaterThanOrEqualTo(AppointmentPropertyPath.StartTime, DateTime.Today);
IsLessThanOrEqualTo restriction2 = new IsLessThanOrEqualTo(AppointmentPropertyPath.EndTime, DateTime.Today.AddDays(1));
And restriction3 = new And(restriction1, restriction2);
FindItemResponse response = service.FindItem(StandardFolder.Calendar, AppointmentPropertyPath.AllPropertyPaths, restriction3);
for (int i = 0; i < response.Items.Count; i++)
{
if (response.Items[i] is Appointment)
{
Appointment appointment = (Appointment)response.Items[i];
Response.Write("Subject = " + appointment.Subject);
Response.Write("StartTime = " + appointment.StartTime);
Response.Write("EndTime = " + appointment.EndTime);
Response.Write("Body Preview = " + appointment.BodyPlainText);
Response.Write("----------------------------------------------------------------");
}
}
}
%>