Call asmx webservice from MVC view

People, I'm trying to call the asmx service from an MVC view.

The code in asmx looks like this:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class MyService : WebService
{
    [WebMethod]
    public string SomeMethod()
    {
        return "hello world";
    }
}

ajax call in mvc view:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "MyService.asmx/SomeMethod",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert('success');
        },
        error: function (e) {
            alert('error: ' + e.msg);
        }
    });
});

But I always get the error "undefined". With fiddler turned on, I realized that the web service was not found (the resource was not found).

Any help appreciated

EDIT:

Ok, I earned it. I needed to add the following line to my RouteConfig:

routes.IgnoreRoute("{*x}", new { x = @".*\.asmx(/.*)?" }); 
+3
source share

All Articles