0

我通常使用 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("----------------------------------------------------------------");
            }
        }

    }


     %>
4

2 回答 2

3

ASP.NET 是您正在寻找的答案。

于 2012-06-04T16:41:34.390 回答
0

好的,一个快速而肮脏的 asp.net 网页示例。基本上你的 Main 变成了 PageLoad。确保您的页面代码继承 System.Web.UI.Page(如果您使用的是 VS2008 或 VS2010,那么它会为您处理好。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sample
{
    public partial class Sample: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            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];

                    lblSubject.Text = "Subject = " + appointment.Subject;
                    lblStartTime.Text = "StartTime = " + appointment.StartTime;
                    lblEndTime.Text = "EndTime = " + appointment.EndTime;
                    lblBodyPreview.Text = "Body Preview = " + appointment.BodyPlainText;

                }
            }


        }
        catch (ServiceRequestException ex)
        {
            lblError.Text= "Error: " + ex.Message;
            lblXmlError.Text = "Error: " + ex.XmlMessage;
            Console.Read();
        }
        catch (WebException ex)
        {
            lblWebError.Text = "Error: " + ex.Message;
        }
    }
}

}

那么你的viewpage可能是这样的。确保 CodeBehind 指向带有代码的类,以完成页面的所有繁重工作。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs"     Inherits="SecureCareEnrollment.WebForms.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <label id="lblSubject" runat="server"></label><br />
            <label id="lblStartTime " runat="server"></label><br />
            <label id="lblEndTime " runat="server"></label><br />
            <label id="lblBodyPreview" runat="server"></label><br />
----------------------------------------------------------------<br />
            <label id=lblError" runat="server"></label><br />
            <label id=lblXmlError" runat="server"></label><Br />
            <label id=lblWebError" runat="server"></label>
    </div>
    </form>
</body>
</html>

显然,您可以/应该对母版页、样式和一堆其他东西做更多的事情。这只是一个基本的香草代码隐藏和页面。

于 2012-06-05T13:09:18.340 回答