Click here to Skip to main content
15,669,580 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
500 Internal Server Error

jQuery Ajax not calling webmethod after url rewriting

contain following colde

public string HelloWorld() {
return "Hello World";
-------------my jquery code -------------

<script type="text/javascript">
    $(document).ready(function ()

            type: "POST",
            url: 'WebService.asmx/HelloWorld',
            data: {},
            contentType: "application/json; charset=utf-8",
            success: countItem
        function countItem(data, status, xhr)





it is working fine but when i use url rewrite or url rooting it give error

original path
it will work
but after url rewrite or url rooting

so it gives error

What I have tried:

void Application_Start(object sender, EventArgs e)
// Code that runs on application startup
// Code that runs on application startup


void RegisterRoute(System.Web.Routing.RouteCollection routes)

routes.Add("Products", new System.Web.Routing.Route("{Products}/{category}", new CategoryRouteHandler()));

public class CategoryRouteHandler : IRouteHandler
public CategoryRouteHandler() { }

public IHttpHandler GetHttpHandler(RequestContext requestContext)
// AdventureWorksEntities awe = new AdventureWorksEntities();
string cat = requestContext.RouteData.Values["category"] as string;
string pageval = requestContext.RouteData.Values["Products"] as string;
//int catid = awe.ProductCategories.Where(x => x.Name == cat).FirstOrDefault().ProductCategoryID;
HttpContext.Current.Items["catid"] = cat;
if (pageval.ToLower().ToString().Trim() == "test")

return BuildManager.CreateInstanceFromVirtualPath("~/test1.aspx", typeof(Page)) as Page;
return BuildManager.CreateInstanceFromVirtualPath("~/Products.aspx", typeof(Page)) as Page;

// string vartualpath=BuildManager.CreateInstanceFromVirtualPath("~/Products.aspx", typeof(Page)).ToString();
//return BuildManager.CreateInstanceFromVirtualPath("~/category.aspx", typeof(Page)) as Page;
Updated 17-May-18 2:32am


From your javascript code what I sense is While you are rewriting or mapping , the url is changed and the same url is not supplied in the ajax url.

your url:

but after mapping as the url is changed, ajax is unable to find the same.

So, I think your url in ajax should be:


Note: please put a "/" in the beginning of the url sothat it will be able route properly when a method of a different controller is called.

Please try and post queries if any.

Share this answer
I think the same too.
I wrote a solution on Stack Overflow about this issue, check this link... - jQuery Ajax not calling webmethod after url rewriting - Stack Overflow[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900