2016-09-28 38 views
-2

我需要使用属性[WebMethod]调用C#方法,它不应该使用MVC,WebForms,API。它应该是一个干净的c#类(.CS),HTML文件。如何通过jQuery Ajax调用c#类中的方法而不使用MVC,WebForms概念

这里是我的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> 
+2

*不应该使用MVC行,Web窗体,API * **你想要什么?为什么?** –

+0

显示'GetMessage'方法 –

+0

嗨,请参阅上面的代码。如果只用一个属性就可以帮助我。如果没有,请解释为什么它不起作用! – Prashanth

回答

1

我认为你需要一个.net webservice。工作原理和你想要的不一样。不是webform/MVC ..让我们用WebService1.asmx和HTMLPage1.htm在同一个目录下。

确保您取消注释[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 StackOverflow_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> 
+0

您是否阅读过这个问题? OP不应该希望使用Web服务。只需要类库中的一个方法,并想从ajax调用。他想知道ASP.NET什么时候不在那里,那时人们是如何工作的,*在早些时候他们是如何在没有MVC或其他概念的情况下使用它的。*他并没有特别要求我如何处理其他概念的MVC或网络表格 –

+0

是@Div。你是对的。 – Prashanth

+0

如果我是对的,那么为什么你接受这个答案呢? –

2

我知道这可能不是你要找的答案。但是从你的问题来看,我觉得你真的不知道客户机 - 服务器体系结构是什么,什么是服务器和什么是客户机。

我会建议您理解图层,然后尝试为您的情况找到解决方案。

直接回答你的问题是“不可能”。但要理解为什么?您需要了解客户端 - 服务器系统的体系结构。你可以从这里开始 -

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

+0

好的答案 - 为了更好地询问/搜索他需要的内容,更需要了解网络技术 – Kevin