如何获取 C# 脚本并使其输出 html 页面
本文关键字:输出 页面 html 脚本 何获取 获取 | 更新日期: 2023-09-27 18:36:11
我通常用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("----------------------------------------------------------------");
}
}
}
%>
ASP.NET 就是
您正在寻找的答案。
好的一个
快速而肮脏的 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>
显然,您可以/应该对母版页、样式和一堆其他东西做更多的事情。 这只是一个基本的原版代码隐藏和页面。