-2

I need to call a C# method by using attribute [WebMethod] and it should not use MVC, WebForms, API. It should be a clean c# class (.CS), HTML file.

Here is my WebMethod:

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

Here is my ajax code:

<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>
Prashanth
  • 111
  • 1
  • 10
  • 2
    *it should not use MVC, WebForms, API* **What do you want? and why?** – Divyang Desai Sep 28 '16 at 04:53
  • Show `GetMessage` method – Divyang Desai Sep 28 '16 at 04:55
  • Hi, Please see the above code. And help me out if it works with just an attribute. if not, please explain why it won't work ! – Prashanth Sep 28 '16 at 04:55
  • I just want to know that, is it possible to make ajax call to a c# method from HTML page – Prashanth Sep 28 '16 at 04:58
  • 1
    Code is not self-aware. Having a method in a .cs file isn't enough to run a web server. You need something to figure out how to *listen* for a request, and to decide what method to invoke (and how to serialize the result). The libraries you explicitly say you don't want to use do that for you. So you'll need to find a library you *can* use, or write it all from scratch. – Rob Sep 28 '16 at 05:01
  • Ok. But in MVC, how it listens to a controller ? So, I tried with above code. I also have doubt, in earlier days how they were using it without MVC or other concepts. – Prashanth Sep 28 '16 at 05:04
  • See how [in earlier days how they were using it without MVC or other concepts](http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx), But it's webforms. – Divyang Desai Sep 28 '16 at 05:06
  • repost: _it should not use MVC, WebForms, API_ *What do you want?* and why? – Manoz Sep 28 '16 at 05:17

2 Answers2

2

I know this is probably not the answer you are looking for. But from your question it seems to me that you really don't know what a Client-Server Architecture is and what is a server and what is a client.

I would recommend understanding the layers then try to find a solution for your situation.

The direct answer to your question is "Impossible". But to understand why? you need to understand the architecture of a Client-Server system. You can start from here -

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

Specific links for IIS -

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

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

Asp.net page life cycle -

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

brainless coder
  • 6,310
  • 1
  • 20
  • 36
  • Good answer - what the op really needs is to understand web technologies more in order to better ask / search for what he needs – Kevin Sep 28 '16 at 14:13
1

I think you need a .net webservice .Works same like you want ..not webform/MVC ..Let WebService1.asmx and HTMLPage1.htm are in same directory.

Make sure you uncomment the line [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;
        } 
    }
}

and 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>
Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45
  • Have you read the question? OP should not want to use web services. Just want one method in class library and want to call from ajax. He want to know when ASP.NET was not there, at that time how people working.., *in earlier days how they were using it without MVC or other concepts* AND he has not asked particularly how can i do with other concept instead of MVC or webforms – Divyang Desai Sep 28 '16 at 07:40
  • If I'm right then why you accept this as an answer? – Divyang Desai Sep 28 '16 at 09:18