0

This is my jQuery code that call Controller from timeclock.controller class library project:

  function getCountries()
  {
           // alert();
            var obj = {};
            obj.countryName = '';
            $.ajax({
                type: 'GET',
                url: '/Business/GetCountries',
                dataType: 'json',
                async: false,
                success: function (data) {

                    console.log(data);



                },
                error: function (err) {

                    console.log(err);
                }
            });

  }

This is the controller from another class library project that I want to call:

namespace timeclock.controller
{
    class BusinessController : System.Web.Mvc.Controller
    {
        [HttpGet]
        [Route("Countroller/GetCountries")]
        public JsonResult GetCountries(string countryName)
        {
            //List<countries_model> countrylist = null;
            var myresult = "{'name':'John', 'age':31, 'city':'New York' }";
            return Json(myresult,JsonRequestBehavior.AllowGet);
        }
    }
}

This is a route config file of my main project:

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

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

            routes.MapMvcAttributeRoutes();
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • What is the error found in your current setup? Is that plain 404 or other error code present? – Tetsuya Yamamoto Jan 22 '18 at 06:09
  • its give me 404 error "the server response with status 404" –  Jan 22 '18 at 06:09
  • `class BusinessController` => I think this class should be set as `public`. Also `[Route("Countroller/GetCountries")]` should be set as `[Route("Business/GetCountries")]` and use `url: '@Url.Action("GetCountries", "Business")'`. – Tetsuya Yamamoto Jan 22 '18 at 06:13
  • i have change the code as per your guideline but its give me error of 404 –  Jan 22 '18 at 06:20
  • i got the solution thank you @Tetsuya Yamamoto –  Jan 22 '18 at 07:06

2 Answers2

0

Finally I get the solution just remove action in route config

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

The issue is that you have your routes defined in the wrong order. MapMvcAttributeRoutes must come before the Default route if you expect any [Route] attributes having URLs with 1, 2, or 3 segments to function.

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

    routes.MapMvcAttributeRoutes();

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

Reference: Why map special routes first before common routes in asp.net mvc?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212