0

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 }
);
Jyina
  • 2,530
  • 9
  • 42
  • 80

1 Answers1

1

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();
    }
}
Rafi Henig
  • 5,950
  • 2
  • 16
  • 36
  • Is it possible to escape the special characters in the id "/". Because I have "/" in id value, it was not finding the route so I have to change it to var url = sc.baseURL + 'customer?id=' + $stateParams.custid; – Jyina Aug 19 '20 at 21:34
  • https://stackoverflow.com/questions/6328713/urls-with-slash-in-parameter/6328758#6328758 – Rafi Henig Aug 19 '20 at 21:43