你如何采用C#脚本并使其输出一个html页面?

时间:2012-06-04 16:30:30

标签: c# iis

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

    }


     %>

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

确定一个快速而又脏的asp.net网页示例。基本上你的主要成为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;
        }
    }
}

}

然后您的视图可能是这样的。确保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>

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