locked
Asp.Net Url Re-Writing RRS feed

  • Question

  • User491718545 posted

    Hi m using ASp.Net Url Re-writing

    when m using this i il get url like this 

    http://localhost:10869/Customer

    but webmethod wont call

    $.ajax({
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    url: 'CustomerMaster.aspx/InsertMethod',---Here How to call webmethod,its wont called when m using asp.net url rewriting
    data: "{'FirstName':'" + $('#<%=txt_FirstName.ClientID%>').val()'}",
    async: false,
    success: function (response) {

    }

    });

    [WebMethod]

    public static string(String FirstName)

    {

    return "";

    }

    Thank you


    Saturday, November 16, 2013 3:04 AM

Answers

  • User1734617369 posted

    Hi,

    I have created a simple page that uses Asp.Net url rewriting, tha page is like:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication3.WebForm3" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#testBtn').click(function () {
                    $.ajax({
                        type: 'POST',
                        contentType: "application/json; charset=utf-8",
                        url: 'WebPage3.aspx/InsertMethod',
                        data: "{'FirstName':'" + $('#<%= txt_FirstName.ClientID %>').val() + "'}",
                        async: false,
                        success: function (response) {
                            alert(response.d);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Hejsan!
            <asp:TextBox runat="server" ID="txt_FirstName" />
            <input type="button" id="testBtn" />
        </div>
        </form>
    </body>
    </html>

    and the code behind for the page:

    using System;
    using System.Web.Services;
    
    namespace WebApplication3
    {
        public partial class WebForm3 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [WebMethod]
            public static string InsertMethod(string FirstName)
            {
                return FirstName;
            }
        }
    }

    and I have mapped this in my Global.asax like this:

    using System;
    using System.Web.Routing;
    
    namespace WebApplication3
    {
        public class Global : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                RegisterRoutes(RouteTable.Routes);
            }
    
            void RegisterRoutes(RouteCollection routes)
            {
                routes.MapPageRoute("WebForm3", "Customer", "~/webform3.aspx");
            }
        }
    }

    This example works and the web method is called. I can see that in your javascript code you have:

    data: "{'FirstName':'" + $('#<%=txt_FirstName.ClientID%>').val()'}",

    and that is slightly wrong, it should be:

    data: "{'FirstName':'" + $('#<%=txt_FirstName.ClientID%>').val() + "'}",

    Best regards
    Johan

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 16, 2013 3:39 PM

All replies

  • User1734617369 posted

    Hi,

    I have created a simple page that uses Asp.Net url rewriting, tha page is like:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication3.WebForm3" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#testBtn').click(function () {
                    $.ajax({
                        type: 'POST',
                        contentType: "application/json; charset=utf-8",
                        url: 'WebPage3.aspx/InsertMethod',
                        data: "{'FirstName':'" + $('#<%= txt_FirstName.ClientID %>').val() + "'}",
                        async: false,
                        success: function (response) {
                            alert(response.d);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Hejsan!
            <asp:TextBox runat="server" ID="txt_FirstName" />
            <input type="button" id="testBtn" />
        </div>
        </form>
    </body>
    </html>

    and the code behind for the page:

    using System;
    using System.Web.Services;
    
    namespace WebApplication3
    {
        public partial class WebForm3 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [WebMethod]
            public static string InsertMethod(string FirstName)
            {
                return FirstName;
            }
        }
    }

    and I have mapped this in my Global.asax like this:

    using System;
    using System.Web.Routing;
    
    namespace WebApplication3
    {
        public class Global : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                RegisterRoutes(RouteTable.Routes);
            }
    
            void RegisterRoutes(RouteCollection routes)
            {
                routes.MapPageRoute("WebForm3", "Customer", "~/webform3.aspx");
            }
        }
    }

    This example works and the web method is called. I can see that in your javascript code you have:

    data: "{'FirstName':'" + $('#<%=txt_FirstName.ClientID%>').val()'}",

    and that is slightly wrong, it should be:

    data: "{'FirstName':'" + $('#<%=txt_FirstName.ClientID%>').val() + "'}",

    Best regards
    Johan

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 16, 2013 3:39 PM
  • User491718545 posted

    Thank you for ur answer..i had another one problem 

    Without ASp.Net Url Re-writing

    when i run program   http://localhost:10869/CustomerMaster.aspx  button Event working-Everthing working fine

    Eg:

        <input type="button" class="btn-glow primary" id="btnSubmit"   value="Add New" />

             $('#btnSubmit').on('click', function () {
             window.location.href = "CustomerMasterView.aspx";
           });

    Using  ASp.Net Url Re-writing url wil be http://localhost:10869/CustomerMaster

    button event wont fire,dont't know why its not fire btnclick event in Url-Rewriting...

    give me solution pl


    thanks

    Sunday, November 17, 2013 11:00 PM
  • User1734617369 posted

    Hi,

    I have tested the code that you provided for the button and that works fine for me when using URL-rewriting, could you provide more of the code so that we can see if anything else needs to be changed to make it work?

    Best regards
    Johan

    Monday, November 18, 2013 2:21 PM
  • User491718545 posted

    Hi,

    thank you 4 ur answer..i create a separte form write  your code, working fine..when i used master page does not work

    creating MasterPage.master.cs--give reference inMasterPage.master.cs

     <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js" type="text/javascript"></script> <script type="text/javascript"> 
    In my customerMaster.cs

    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

     <input type="button" class="btn-glow primary" id="testBtn" value="Add New" />
    <Script>
    $(document).ready(function () { $('#testBtn').click(function () { $.ajax({ type: 'POST', contentType: "application/json; charset=utf-8", url: 'WebPage3.aspx/InsertMethod', data: "{'FirstName':'" + $('#<%= txt_FirstName.ClientID %>').val() + "'}", async: false, success: function (response) { alert(response.d); } }); }); }); 
    </script>
    </asp:Content>
    WHEN I USED MASTER PAGE MASTER ,PAGE REFERENCE DO NOT TAKE CUSTOMER FORM ,so button event do not fire
    Thanks.
    Wednesday, November 20, 2013 11:44 PM
  • User1734617369 posted

    Hi,

    I don't exactly understand what the problem you have is, here is an example using a master page.

    The master page:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="WebApplication3.MasterPage" %><!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">        
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    </html>

    and the content web page:

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="CustomerMaster.aspx.cs" Inherits="WebApplication3.CustomerMaster" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <input type="button" class="btn-glow primary" id="testBtn" value="Add New" />
        <asp:TextBox runat="server" ID="txt_FirstName" />
        <script>
            $(document).ready(function () {
                $('#testBtn').click(function () {
                    $.ajax({
                        type: 'POST',
                        contentType: "application/json; charset=utf-8",
                        url: 'CustomerMaster.aspx/InsertMethod',
                        data: "{'FirstName':'" + $('#<%= txt_FirstName.ClientID %>').val() + "'}",
                        async: false,
                        success: function (response) {
                            alert(response.d);
                        }
                    });
                });
            });
        </script>
    </asp:Content>

    and the code for the content web page:

    using System;
    using System.Web.Services;
    
    namespace WebApplication3
    {
        public partial class CustomerMaster : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            [WebMethod]
            public static string InsertMethod(string FirstName)
            {
                return FirstName;
            }
        }
    }

    and I have mapped the url in the Global.asax.cs like this:

    using System;
    using System.Web.Routing;
    
    namespace WebApplication3
    {
        public class Global : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                RegisterRoutes(RouteTable.Routes);
            }
    
            void RegisterRoutes(RouteCollection routes)
            {
                routes.MapPageRoute("CustomerMaster", "CustomerMaster", "~/customermaster.aspx");
            }
        }
    }






    Thursday, November 21, 2013 2:02 PM