Kevin Simper
Kevin Simper

Reputation: 1687

How do you take a C# script and make it output a html page?

I normally code in PHP and Python, but in this case i have to make it in C#.

I have this code, It works really good. It is a console application.

But how can you make it to a C# .net so that is can put it on a IIS?

Basicly instead of outputting it to a console, it should just write it to the browser.

I have tried to search for C# Web, but could not find anything.

Thanks for the help!

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();
            }
        }
    }
}

EDIT: I have tried to make it a asp.net page But it do not print anything to the screen.

<%@ 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("----------------------------------------------------------------");
            }
        }

    }


     %>

Upvotes: 0

Views: 991

Answers (2)

Brian
Brian

Reputation: 2229

ok a quick and dirty asp.net webpage sample. Basically your Main becomes PageLoad. Be sure that your code behind pages inherit System.Web.UI.Page (if you are using VS2008 or VS2010 then it is taken care of for you.

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;
        }
    }
}

}

then your viewpage could be like this. Be sure that the CodeBehind is pointing to the class with the code to do all the heavy lifting for the page.

<%@ 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>

obviously there is a lot more you can/should do with master pages, styling and a bunch of other stuff. This is just a basic vanilla codebehind and page.

Upvotes: 0

idish
idish

Reputation: 3260

ASP.NET is the answer you're looking for.

Upvotes: 3

Related Questions