1

I have this page method (web method):

public partial class MyTest : System.Web.UI.Page
{
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)]
    public static string Greeting()
    {
        return "Hi!";
    }
}

Is it possible to type MyTest.aspx/Greeting in the browser and get the actual string? It works via Ajax (method GET) but doesn't seem to work directly from browser (the .aspx page is returned instead).

I know about MVC / Web Api, but I cannot use them.

Related questions:

Possible to invoke ASMX service with parameter via url query string?

Testing a web method (with browser/Fiddler)

turdus-merula
  • 8,546
  • 8
  • 38
  • 50
  • 1
    Why use ASMX in the first place? Why not Web API? – mason Jun 09 '17 at 16:46
  • @mason, legacy project - one of the constraints being to not reference extra DLLs (no NuGet package manager and so on). Btw, ASPX, not ASMX. – turdus-merula Jun 09 '17 at 17:04
  • 1
    Whether it's ASPX or ASMX is really no different. The mechanism is the same. WebMethods suck. Microsoft is trying to phase them out. Why not create a generic handler .ashx? And why can't you use NuGet packages? – mason Jun 09 '17 at 17:13
  • @mason, in a generic handler you don't have (at least rudimentary) routing and parameter binding out-of-the box. You have to parse the URI, the query string and so on. Doable, anyway. I think it is a plausible idea. Thanks. – turdus-merula Jun 09 '17 at 17:18
  • 1
    Can you move the method into an ASMX file? Then it should work if you also add the Web.Config settings mentioned in the first link. – Greg Jun 09 '17 at 18:41
  • @greg, that would not return exactly the string, but an XML-formatted string instead: ` Hi!` – turdus-merula Jun 09 '17 at 21:17
  • 1
    True, also the switching to ResponseFormat.Json doesn't work. I think using ASHX like @mason suggested would be the best solution. – Greg Jun 11 '17 at 14:30

0 Answers0