Asp.net mvc MVC控制器中routeconfig之后的调用函数
我用MVC做了一个路由配置。路线的定义如下:Asp.net mvc MVC控制器中routeconfig之后的调用函数,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我用MVC做了一个路由配置。路线的定义如下: routes.MapRoute( name: "Box", url: "boxes/{id}", defaults: new { controller = "Boxes", action = "Index", id = UrlParameter.Optional } ); public ActionResult Index() { //Code here return view(); } 问题是,当我从视图框调用javas
routes.MapRoute(
name: "Box",
url: "boxes/{id}",
defaults: new { controller = "Boxes", action = "Index", id = UrlParameter.Optional }
);
public ActionResult Index()
{
//Code here
return view();
}
问题是,当我从视图框调用javascript函数时,我调用的所有函数都被重定向到索引函数
例如,如果我调用var url=“/box/ReturnPrice”代码>站点不调用此函数,而是调用索引函数
boxesController中的索引函数定义如下:
routes.MapRoute(
name: "Box",
url: "boxes/{id}",
defaults: new { controller = "Boxes", action = "Index", id = UrlParameter.Optional }
);
public ActionResult Index()
{
//Code here
return view();
}
当您调用/Box/ReturnPrice
时,它与您的“Box”路线定义相匹配。框架将把“ReturnPrice
”从url映射到id
参数
您需要定义一个路由约束,它告诉您的id属性是int类型的(在您的例子中,我假设它是int)。此外,您还需要确保存在通用路由定义,以使用controllername/actionmethodname
格式处理正常请求
使用正则表达式定义管线时,可以定义管线约束
routes.MapRoute(
name: "Box",
url: "boxes/{id}",
defaults: new { controller = "Boxes", action = "Index", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
使用此box/ReturnPrice
将转到ReturnPrice操作方法,而box/5
将转到索引操作方法,值5设置为Id param。返回价格的路径在哪里?ReturnPrice
是boxesController中名为public ActionResult ReturnPrice的函数,但从未调用此代码。如果我不使用routeconfig,我可以正确地调用此函数RightlyRight,因此您需要为它添加路由。看起来您只有索引的路由,但我从javascript调用它。我怎么走?它与索引页面的url相同。我理解,它是正确的,但在另一个页面中,我需要将名称作为参数作为字符串。我如何使用字符串来实现这一点?我已经为Box/ReturnPrice添加了另一个routeConfig,现在它可以正常工作了。非常感谢你!