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 8425935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:24:43+00:00 2026-06-10T04:24:43+00:00

I have an Mvc3 project that includes both Mvc and Api controllers. When I

  • 0

I have an Mvc3 project that includes both Mvc and Api controllers. When I run the application without specifying the “controller/action“, the “Default” route is selected and the “home/index” page is rendered. When that page runs, an Ajax call is made using url: “api/controller“, returning json, which is then used to populate a table on the page.

 <script type="text/javascript">
     $(function () {
         var $products = $("#products");
         $.ajax({
             url: "api/products",
             contentType: "json",
             success: function (data) {
                 $.each(data, function (index, item) {
                     $products.append("<tr><td>" + item.ProductCode + "</td>" +
                                         "<td>" + item.Description + "</td>");
                 });
             }
         });
     });
 </script>

However, when a request is made to the same page specifying the “controller/action”, as in “localhost/home/index“, the Ajax call is translated to “/home/api/controller” and of course the request cannot complete or return any results, since the ApiController cannot be found.

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

        routes.MapHttpRoute(
           "DefaultApi",
           "api/{controller}/{id}",
           new { id = RouteParameter.Optional }
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );
    }

Considering that it must be a routing issue, I proceeded to resolve this by adding a route:

        routes.MapHttpRoute(
           "HomeApi",
           "home/api/{controller}/{id}",
           new { id = RouteParameter.Optional }
        );

This was successful. As was my next change to:

        routes.MapHttpRoute(
           "AnyApi",
           "{folder}/api/{controller}/{id}",
           new { id = RouteParameter.Optional }
        );

Which also works but leaves me somewhat skeptical as to whether it is the correct way to handle combining WebApi with Mvc.

Is this the correct way to handle this? Or are there better alternatives?

  • 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-10T04:24:44+00:00Added an answer on June 10, 2026 at 4:24 am

    Considering that it must be a routing issue, I proceeded to resolve this by adding a route:

    No, that’s not a routing issue at all. Your routes are perfectly fine:

    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapHttpRoute(
           "DefaultApi",
           "api/{controller}/{id}",
           new { id = RouteParameter.Optional }
        );
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );
    }
    

    Your issue stems from the fact that you have hardcoded the url in your javascript instead of using url helpers to generate it. You should absolutely never hardcode an url inside an ASP.NET MVC application. You should always use url helpers.

    So:

    <script type="text/javascript">
        $(function () {
            var $products = $("#products");
            var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" })';
            $.ajax({
                url: url,
                success: function (data) {
                    $.each(data, function (index, item) {
                        $products.append("<tr><td>" + item.ProductCode + "</td>" +
                                             "<td>" + item.Description + "</td>");
                    });
                }
            });
        });
    </script>
    

    Also notice that I have removed the contentType: 'json' from your AJAX call because first the correct content type is contentType: 'application/json' and second in this case you are not sending any data in the request, so you shouldn’t be setting it to application/json.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC3 project that uses a tab strip on certain pages.
I have some C# code in my ASP.NET MVC3 project that's throwing an exception
I have a project that uses MVC 3 and Entity Franework. The site works
I have a MVC3 project that uses the Entity Framework and Ninject v2.2, and
I have an ASP.NET MVC 3 project that I just created using the project
I have a new MVC3 project with one Controller called PublicController.cs which contains 4
For a current MVC3 project I have a model that has multiple pages for
I have a MVC3 C# project that I have a model of FoodItem and
I have an ASP.MVC 3 project that I am trying to get building on
Prehistory: I have a simple ASP.NET MVC3 application. In the project file i've turned

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.