0

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);
    }
});  
Community
  • 1
  • 1
xoail
  • 2,978
  • 5
  • 36
  • 70
  • Are you using URL Rewrite anywhere? That has blown up my world a couple million times. :) – Brandon Osborne Dec 06 '11 at 06:37
  • how are you consuming the webservice?. if via JS please post the code? – Pankaj Kumar Dec 06 '11 at 06:47
  • @coderpros I am not using any url rewrites – xoail Dec 06 '11 at 07:00
  • @PankajKumar basically even if I point this http://localhost/service.asmx/MethodName in web browser, I get document not found. I have attached the JS code to question. – xoail Dec 06 '11 at 07:02
  • 1
    Seems like I had this same problem, and it came down to making the WebMethod-annotated method `static`. – Tieson T. Dec 06 '11 at 07:15
  • @TiesonT. changing it to static hides the operation. I dont see it any more when I point to http://localhost/service.asmx – xoail Dec 06 '11 at 07:24
  • possible duplicate of [ASMX service works on development server, returns 404 when deployed to IIS 7.5](http://stackoverflow.com/q/5416020/50447) – Rowland Shaw Jun 11 '12 at 14:21

2 Answers2

2

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.

Pankaj Kumar
  • 1,748
  • 6
  • 28
  • 41
  • 1. Unfortunately it doesnt hit the break point. I think its breaking before the service method call. 2. Invoke button results in document not found. 3. I get document not found and no stack trace. – xoail Dec 07 '11 at 03:13
  • Managed to get stack trace clicking invoke: Exception Details: System.Web.HttpException: The HTTP verb POST used to access path '/services.asmx/Coupons' is not allowed. [HttpException (0x80004005): The HTTP verb POST used to access path '/services.asmx/Coupons' is not allowed.] System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) +2875994 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8681506 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 – xoail Dec 07 '11 at 03:25
  • 1
    @xoail : have a look at this link http://dev.ektron.com/forum.aspx?g=posts&t=16404 and see if this is the problem in your case. If the invoke is not calling the method properly then i think it is a configuration error perhaps. Also please post the stack trace in the question as an edit. :) – Pankaj Kumar Dec 07 '11 at 05:42
  • that link did it.. I removed * mapping from iis and I can see that my service is working fine. But unfortunately it has removed the route the urls without .aspx extension. Previously I was able to navigate through the site like site.com/aboutus and site.com/products, now I have to postfix .aspx at the end to make it work... is this expected? can I not have extensionless urls and webservices working together? – xoail Dec 07 '11 at 17:50
  • if that is the case then again reset the * mapping and make sure to check if .asmx is mapped in the iis for verb=*(asterisk).if Not added add one. Also, if using .net 4.0 you can try adding this mapping in your web.config under the httphandlers section under System.web:- and see if it works – Pankaj Kumar Dec 08 '11 at 05:11
  • .asmx is mapped to GET, HEAD, POST ... httphandler is already in place in web.config – xoail Dec 08 '11 at 16:21
  • @xoail : some things to clarify : did u remove the * or *. mapping and what version of IIS and /aboutus and /products are html files ? – Pankaj Kumar Dec 13 '11 at 05:04
  • have a look at this article. http://www.codeproject.com/KB/aspnet/extensionless.aspx – Pankaj Kumar Dec 13 '11 at 05:39
0

Using F11 to Debug your code,try to see what happend when you call webservice,and see what returned "ViewManager.RenderView("~/Modules/CouponsPromotions/CouponCenter.ascx");". hopes everything goes well.