Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:19:04+00:00 2026-06-13T06:19:04+00:00

My ajax calls the wrong method in .net mvc 4 and I can’t figure

  • 0

My ajax calls the wrong method in .net mvc 4 and I can’t figure out why.

My ajax:

function addItem(id, ammount) {
$.ajax({
    url: "/Shoppingcart/AddItem?id="+id+"&ammount="+ammount,
    type: "post",
    cache: false,
    success: function (result) {
        alert("SUCCESS!!!");
    },
    error: function (textStatus, errorThrown) {
        window.console.log(textStatus, errorThrown);
    }
});
}

My mvc controller:

public class ShoppingcartController : Controller
{
    //
    // GET: /Shoppingcart/

    public ActionResult Index()
    {
        // Method 1
    }

    [HttpPost]
    public ActionResult AddItem(int id = -1, int ammount = 0)
    {
        return Redirect("~/Home");
    }
}

My first method is getting called by the ajax, which is strange since I call /Shoppingcart/AddItem
Why is this happening and what should I do to make it work?

Solution:
The problem was not in the method calling but in the route stack. Apperantly the order in wich the routes are defined influences their importancy. The most specific route should always be the first route to be declared.

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Index",
            url: "{controller}/{id}",
            defaults: new { controller = "Home", action = "Index" },
            constraints: new { id = @"\d+" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "ControllerOnly",
            url: "{controller}",
            defaults: new { controller = "Home", action = "Index", id = 0 }
        );
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:19:05+00:00Added an answer on June 13, 2026 at 6:19 am

    The problem is with your routing because the configured routes are matched in order.

    So first you should put the more specify ones and at the end the more generic ones.

    You have to most generic route with url: "{controller}" first which matches the url localhost:1862/Shoppingcart/AddItem?id=18&ammount=1 and uses the action = "Index" instead of the action AddItem.

    To fix it change your route order to:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    
    routes.MapRoute(
        name: "Index",
        url: "{controller}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    
    routes.MapRoute(
        name: "ControllerOnly",
        url: "{controller}",
        defaults: new { controller = "Home", action = "Index", id = 0 }
    );
    

    To make urls like /Product/18 route correctly you need to change your "Index" route with using a constraint on id instead of UrlParameter.Optional and you need to put it before the "Default" route:

    routes.MapRoute(
        name: "Index",
        url: "{controller}/{id}",
        defaults: new { controller = "Home", action = "Index" },
        constraints: new {id = @"\d+"}
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure what I'm doing wrong but I can't get my JQuery AJAX call
I have two Ajax calls. I need to make sure that a JS function
Does Asp.Net require ActiveX to make Ajax calls? I suspect it does because without
I am trying to call ASP.net 1.1 service in asp.net using Jquery, can't figure
I'm currently rewriting all my ajax calls to use the jquery method (much cleaner
I'm wondering what is the best method to handle AJAX calls with jQuery? Right
How can I pass custom error information from an ASP.NET MVC3 JsonResult method to
I have two ajax calls. Both are sync. Both call methods from one controller.
i am making ajax calls with jquery like this http://pastie.org/860837 and sometimes i get
I'm making AJAX calls to a server that sometimes return unparseable JSON. The server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.