0

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

    }


     %>
3
  • using mvc, the stuff going on in your Main would equate to an actionMethod in the controller and your console.writelines can be accomplished with ViewData. I suggest google on c# mvc and you will find lots of sample projects. Alternatively google on asp.net and you will see sample projects for webforms apps Commented Jun 4, 2012 at 17:46
  • @Brian Thank you, I will try to do that. I also tried to make the script into a asp.net page with C#. Commented Jun 5, 2012 at 6:37
  • if you know which direction you want to go, MVC or asp.net I can give some samples. MVC is more fun, but the learning curve is a bit steeper. asp.net you can get up to speed faster but it is boring to do :-) Commented Jun 5, 2012 at 12:37

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

7 Comments

Yeah, something with starting the page with <%@ Page Language="C#"%>. Can i just take the code inside Main and the find a way to include the References?
You can't work on a server based application like ASP.NET in console app or win forms app. check this link for more information w3schools.com/aspnet/default.asp
I love w3schools, and i use it alot. I normally program in PHP and Python. I am interesting in how to turn this example into asp.net that outputs it to HTML instead of Console.Write, so that i can put on my IIS. Thanks for your help!
Response.Write("Hello world");
I know how to write a bit asp.net, which writes to the screen. I know how to write c#, that makes a console application. What i do not know is how to take a c# application and turn it into a asp.net page.
|
0

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.

1 Comment

If possible use visual studio. A lot of the plumbing tying the page and the codebehind together is done for you then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.