So I am pulling my hair out over this. Environment: ASP.net web form C# 2008.
I already have this solution in place.
I created an asmx webservice like the following (see code) and I am trying to access it via the browser. Basically when I point to http://localhost/service.asmx it works fine and I can see the available operations and stuff.. I can also see operation in detail at the url http://localhost/service.asmx?op=MethodName but the problem comes when I actually try the service or invoke. I get http://localhost/service.asmx/MethodName is not found.
Any idea what could be going wrong?
[WebService(Namespace = "http://locahost/")]
[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 WellnessService : System.Web.Services.WebService
{
[WebMethod]
public string MethodName()
{
return ViewManager.RenderView("~/Modules/CouponsPromotions/CouponCenter.ascx");
}
}
public class ViewManager
{
public static string RenderView(string path)
{
return RenderView(path, null);
}
public static string RenderView(string path, object data)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
if (data != null)
{
Type viewControlType = viewControl.GetType();
FieldInfo field = viewControlType.GetField("Data");
if (field != null)
{
field.SetValue(viewControl, data);
}
else
{
throw new Exception("View file: " + path + " does not have a public Data property");
}
}
pageHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
}
This is how I am calling via JS
$(document).ready(function() {
$('#liCoupons').click(function() {
$.ajax({
type: "POST",
url: "/services.asmx/MethodName",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#result').html(msg.d);
},
error: ajaxFailed
//error
});
});
function ajaxFailed(xmlRequest) {
alert(xmlRequest.status + ' \n\r ' +
xmlRequest.statusText + '\n\r' +
xmlRequest.responseText);
}
});
I just copy pasted your partial code, added a web user control(only has static HTML “hello world”) of my own and tested the webservice and it works fine.
couple of things to check.
1) Debug and Step through code to see if web service is running all server code properly without any exceptions
2) there is a Invoke button in http://localhost/service.asmx?op=Coupons part. test if it works correctly. if yes i think the problem lies somewhere else
3) Also please review the error and its stacktrace to get a better idea. Post the stacktrace here with full error also in question.
hope this helps.