问题描述
我试图按以下方式调用MVC路由,该路由应转到客户控制器和索引视图。但是下面的代码没有击中客户控制器。感谢您对我在这里做错的任何输入。
var url = sc.baseURL + 'customer/' + $stateParams.custid;
var w = $window.open(url,'_blank');
MVC控制器:
public class CustomerController : Controller
{
public ActionResult Index(string custid)
{
return View();
}
}
我的MVC默认路由如下。
routes.MapRoute(
name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional }
);
解决方法
由于默认路由模板为{controller=Home}/{action=Index}/{id?}
,因此索引应接受id
而不是custid
(操作参数应与模板路由匹配,以便ModelBinding
可以找到它)
public class CustomerController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}