如何在不使用 MVC、WebForms 概念的情况下通过 jQuery Ajax 在 c# class 中调用方法

How to call a method in c# class by jQuery Ajax without using MVC,WebForms concepts

我需要使用属性 [WebMethod] 调用 C# 方法,它不应使用 MVC、WebForms、API。它应该是一个干净的 c# class (.CS),HTML 文件。

这是我的 WebMethod:

[WebMethod]
public string GetMessage()   // Home.CS 
{           
        return "GOGO";
}    

这是我的 ajax 代码:

<head> //HTML and Ajax 
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 <script>
    function GetMessage() {
        $.get("/Home/GetMessage", function (data) {
            $("p").html(data);
        });
    }
 </script>
</head>
<body>
    <input type="button" onclick="GetMessage()" value="Get Message" />
    <p></p>
</body>

我知道这可能不是您要找的答案。但是从你的问题来看,在我看来你真的不知道什么是客户端-服务器架构,什么是服务器,什么是客户端。

我建议先了解各个层次,然后尝试找到适合您情况的解决方案。

您问题的直接答案是 "Impossible"。但要明白为什么?您需要了解客户端-服务器系统的架构。您可以从这里开始 -

https://en.wikipedia.org/wiki/Client%E2%80%93server_model

IIS 的特定链接 -

https://msdn.microsoft.com/en-us/library/ms178473.aspx

https://msdn.microsoft.com/en-us/library/bb470252.aspx

Asp.net 页面生命周期 -

https://msdn.microsoft.com/en-us/library/ms178472.aspx

我想你需要一个 .net webservice 在同一目录中。

确保取消注释行 [System.Web.Script.Services.ScriptService] WebService1.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

namespace Whosebug_Solve.Services
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetMessage()   // Home.CS 
        {
            //return "GOGO";
            Context.Response.Output.Write("Hello World");
            Context.Response.End();
            return string.Empty;
        } 
    }
}

HTMLPage1.htm

<!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>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<head> //HTML and Ajax 
<title></title>

 <script>
     function GetMessage() {
         //Load jQuery($) to Use 
         $(function() {
             $.get("WebService1.asmx/GetMessage", function (data) {
                 console.log(data);
                 $("p").html(data);
             });
         });

     }
 </script>
</head>
<body>
    <input type="button" onclick="GetMessage()" value="Get Message" />
    <p></p>
</body>
</body>
</html>