2016-09-28 44 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