I m developing website. i want call one ashx file which return all process list on server
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Step: 1 Create the handler and the server side code like the following
<%@ WebHandler Language="C#" Class="Handler" %>
using System; using System.Web; using System.Linq; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "application/json"; var processes = System.Diagnostics.Process.GetProcesses(); System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.Write(serializer.Serialize(processes.Select(p => p.ProcessName))); } public bool IsReusable { get { return false; } } }
Step 2: Give the necessary permissions for your asp.net process / application pool. In development version you can add the impersonation tag in the web.config as a shortcut.
<identity impersonate="true" userName="windows/usernamer" password="password"/>
Step 3: Use the handler's result from the aspx page via ajax or otherwise. An example is as follows using the jquery library.
<%@ Page Title="Home Page" Language="CS" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"> </script> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <ul id="processes"> </ul> <script type="text/javascript"> $(function () { $.get("handler.ashx", function (data) { $.each(data, function (index, element) { $("#processes").append($("<li />").html(element)); }); }); }); </script> </asp:Content>
Hope this helps.