MVC JQuery call from url without action

1 answers
Thread Answer

I have localhost:51775/Book/Index/bookname page and it working without problem. But what I want is to remove Index from url and make it appear as localhost:51775/Book/bookname , I can do the page link as I wanted but this time jquery calls do not work.

I have added a new route to routeconfig as

            new { controller = "Book", action = "Index", id = RouteParameter.Optional }

Jquery call from this url as http://localhost:51775/Book/bookname doesn't work;

var data = new FormData();
    data.append("comment", comment);
    data.append("id", id);

var ajaxRequest = $.ajax({
        type: "POST",
        url: "/Book/AddBookComment",
        contentType: false,
        processData: false,
        data: data

but if I remove routeconfig then it works as http://localhost:51775/Book/Index/bookname

Here is the c# method which is called from jquery;

    public string AddBookComment()
        string comment = Request.Form["comment"];
        string id = Request.Form["id"];

        Int64 idBook = id.ToInt64();
        SuggestBusiness.Instance.AddBookComment(idBook, SessionManager.GetSession().CurrentMember.MemberId, comment);
        return "ok";

localhost:51775/Book/Index/bookname is the working page link, localhost:51775/Book/bookname is the page link that I want it to work, localhost:51775/Book/AddBookComment is the c# method for jquery

What should I do to make that jquery call?


Since you have a route with url: "Book/{id}" it means that in you ajax call, url: "/Book/AddBookComment", will match that route and send you to the Index() method. Either remove the route or you can add another specific route (needs to be before your "Book" route)

   new { controller = "Book", action = "AddBookComment" }
.... // your other routes