Search code examples
angularasp.net-mvcangular-ui-router

How to call MVC controller from angular controller?


I am trying to call MVC route as below that should go to Customer controller and Index view. But the below code is not hitting the Customer controller. Thanks for any input on what I am doing wrong here.

var url = sc.baseURL + 'customer/' + $stateParams.custid;
        
var w = $window.open(url, '_blank');

MVC controller:

public class CustomerController : Controller
{
  public ActionResult Index(string custid)
    {
        return View();
    }
}

I have the MVC default route as below.

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

Solution

  • Since the default routing template is {controller=Home}/{action=Index}/{id?}, Index should accept id rather than custid (Actions param should match the template-routing so the ModelBinding can find it)

    public class CustomerController : Controller
    {
      public ActionResult Index(string id)
        {
            return View();
        }
    }