2

Closed

friendlyurls do not work with webmethods

description

If you page is using webmethods then friendlyurls will not work with the page
Closed May 22, 2013 at 10:44 PM by damianedwards

comments

yamanimurillo wrote Apr 29, 2013 at 7:02 PM

It can be possible to use WebMethods, without making to much changes in the code.

Only add this code after the body tag:
<script> PageMethods.set_path("PageName.aspx"); </script> The problem its than the new page name is "www.someurl.com/PageName", so the line for PageMethods path is "PageMethods.set_path("PageName");" thats why the PageMethods doesn't work.

damianedwards wrote May 22, 2013 at 10:44 PM

The workaround posted by yamanimurillo is a good one. This issue will be present when using routing with pages as well. There isn't really anything for Friendly URLs to do to fix this. People should try to move away from web methods where they can, or use this workaround where they can't.

Alisher2013 wrote Nov 20, 2013 at 7:49 PM

Another workaround is to create webmethod twice, the main should be at asmx file with implementation:
[WebMethod]
    public string Hello()
    {
        return "Helloasdasd";
    }
and in aspx just put prototype of your webmethod:
[WebMethod]
    public static void Hello() { }
In aspx make sure to make it static. And it should work.

Alisher2013 wrote Nov 20, 2013 at 7:53 PM

No implementation is needed in aspx prototype above. And when you run it point your asmx file first.
 PageMethods.set_path('/YourAsmxFile.asmx');
            PageMethods.Hello(function (r) {
                console.log(r);
            },out);
            var out = function () {
                console.log("error");
            }

vishalkaushik wrote Mar 15, 2014 at 3:33 AM

This still does not work for me with latest code. My Page Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        <script> PageMethods.set_path("Default.aspx"); </script>
        <script>
            function helloWorld() {
                PageMethods.HelloWorld("Vishal Kaushik", function (response) {
                    alert(response.Name + "\n" + response.Message);
                });
            }
        </script>
        <div>
            <input type="button" onclick="helloWorld()" value="hello" />
        </div>
    </form>
</body>
</html>
using System;
using System.Web.Script.Services;
using System.Web.Services;

namespace WebApplication4
{
    public class Response
    {
        public string Name { get; set; }
        public string Message { get; set; }
    }

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        [ScriptMethod]
        public static Response HelloWorld(string name)
        {
            Response response = new Response();

            response.Name = name;
            response.Message = String.Format("Hello {0}!", name);

            return response;
        }
    }
}