Url.Action 生成查询而不是参数 URL
Url.Action generates query instead of parameter URL
这是控制器class。我只显示方法签名。
[Authorize]
[RoutePrefix("specification")]
[Route("{action=index}")]
public class SpecificationController : BaseController
{
[HttpGet]
[Route("~/specifications/{subcategoryID?}")]
public ActionResult Index(int? subcategoryID);
[HttpPost]
[Route("get/{subcategoryID?}")]
public JsonResult Get(int? subcategoryID);
[HttpGet]
[Route("~/specifications/reorder/{subcategoryID}")]
public ActionResult Reorder(int subcategoryID);
[HttpGet]
[Route("new/{id?}")]
public ActionResult New(int? id);
[HttpGet]
[Route("edit/{id?}")]
public ActionResult Edit(int id);
[HttpPost]
[ValidateAntiForgeryToken]
[Route("edit")]
public JsonResult Edit(SpecificationJson specification);
[HttpPost]
[Route("moveup")]
public JsonResult MoveUp(int specificationID);
[HttpPost]
[Route("movedown")]
public JsonResult MoveDown(int specificationID);
[HttpDelete]
[Route]
public ActionResult Delete(int id);
}
问题是调用
@Url.Action("index", "specifications", new RouteValueDictionary() { { "subcategoryID", @subcategory.SubcategoryID } })
returns
/specifications?subcategoryID=15
而不是
/specifications/15
为什么会这样?我在那条路线上没有任何类似的方法期待这个!
您必须使用它才能生成跟随 url:/specifications/15
@Url.Action("index", "specifications", new { subcategoryID=subcategory.SubcategoryID })
[Route("~/specifications/{subcategoryID?}")]
public ActionResult Index(int? subcategoryID);
What did I do wrong and how can I revert to using subcategoryID as
parameter name ?
您必须添加另一条路线(在 DEFAULT ROUTE 之前)才能获得另一个 optional
参数:
像这样:
routes.MapRoute(
"SecondRoute",
"{controller}/{action}/{subcategoryID}",
defaults: new { controller = "Home", action = "Index", subcategoryID = UrlParameter.Optional }
);
您生成 URL 的调用不正确。要匹配控制器名称,它应该是 "specification" 而不是 "specifications".
@Url.Action("index", "specification", new { subcategoryID=subcategory.SubcategoryID })
请记住,[Route]
属性中指定的 URL 只是装饰性的。您的路由值必须与控制器名称和操作方法名称相匹配,才能利用该路由生成 URL.
为了让维护代码的人更清楚(并且稍微更快),最好使参数值 Pascal 大小写就像控制器和动作名称一样。
@Url.Action("Index", "Specification", new { subcategoryID=subcategory.SubcategoryID })
Why is this happening?
-------------------------------------------------------------
| Route Key | Route Value | Your Action Request |
|--------------------|---------------|----------------------|
| Controller | Specification | Specifications | No Match
| Action | Index | Index | Match
| subcategoryID | ? | XXX | Match (Always)
-------------------------------------------------------------
要获得路由匹配,@Url.Action
的所有参数都必须匹配路由值字典。问题是路由值字典中没有定义Controller=Specifications
,因为你的实际控制人的名字是SpecificationController
。因此,无论您在 [Route]
属性中输入什么内容,路由值名称都是 Specification
。 URL ~/specifications/{subcategoryID?}
与传出(URL 代)匹配完全无关 - 它仅匹配传入的 URLs 并确定 URL 将生成时的样子
如果要使用 Specifications
而不是 Specification
作为路由值,则需要将操作方法移动到名为 SpecificationsController
的新控制器。也就是说,我看不出它有什么区别,因为最终用户无论如何都看不到路由值名称。
这是控制器class。我只显示方法签名。
[Authorize]
[RoutePrefix("specification")]
[Route("{action=index}")]
public class SpecificationController : BaseController
{
[HttpGet]
[Route("~/specifications/{subcategoryID?}")]
public ActionResult Index(int? subcategoryID);
[HttpPost]
[Route("get/{subcategoryID?}")]
public JsonResult Get(int? subcategoryID);
[HttpGet]
[Route("~/specifications/reorder/{subcategoryID}")]
public ActionResult Reorder(int subcategoryID);
[HttpGet]
[Route("new/{id?}")]
public ActionResult New(int? id);
[HttpGet]
[Route("edit/{id?}")]
public ActionResult Edit(int id);
[HttpPost]
[ValidateAntiForgeryToken]
[Route("edit")]
public JsonResult Edit(SpecificationJson specification);
[HttpPost]
[Route("moveup")]
public JsonResult MoveUp(int specificationID);
[HttpPost]
[Route("movedown")]
public JsonResult MoveDown(int specificationID);
[HttpDelete]
[Route]
public ActionResult Delete(int id);
}
问题是调用
@Url.Action("index", "specifications", new RouteValueDictionary() { { "subcategoryID", @subcategory.SubcategoryID } })
returns
/specifications?subcategoryID=15
而不是
/specifications/15
为什么会这样?我在那条路线上没有任何类似的方法期待这个!
您必须使用它才能生成跟随 url:/specifications/15
@Url.Action("index", "specifications", new { subcategoryID=subcategory.SubcategoryID })
[Route("~/specifications/{subcategoryID?}")]
public ActionResult Index(int? subcategoryID);
What did I do wrong and how can I revert to using subcategoryID as parameter name ?
您必须添加另一条路线(在 DEFAULT ROUTE 之前)才能获得另一个 optional
参数:
像这样:
routes.MapRoute(
"SecondRoute",
"{controller}/{action}/{subcategoryID}",
defaults: new { controller = "Home", action = "Index", subcategoryID = UrlParameter.Optional }
);
您生成 URL 的调用不正确。要匹配控制器名称,它应该是 "specification" 而不是 "specifications".
@Url.Action("index", "specification", new { subcategoryID=subcategory.SubcategoryID })
请记住,[Route]
属性中指定的 URL 只是装饰性的。您的路由值必须与控制器名称和操作方法名称相匹配,才能利用该路由生成 URL.
为了让维护代码的人更清楚(并且稍微更快),最好使参数值 Pascal 大小写就像控制器和动作名称一样。
@Url.Action("Index", "Specification", new { subcategoryID=subcategory.SubcategoryID })
Why is this happening?
-------------------------------------------------------------
| Route Key | Route Value | Your Action Request |
|--------------------|---------------|----------------------|
| Controller | Specification | Specifications | No Match
| Action | Index | Index | Match
| subcategoryID | ? | XXX | Match (Always)
-------------------------------------------------------------
要获得路由匹配,@Url.Action
的所有参数都必须匹配路由值字典。问题是路由值字典中没有定义Controller=Specifications
,因为你的实际控制人的名字是SpecificationController
。因此,无论您在 [Route]
属性中输入什么内容,路由值名称都是 Specification
。 URL ~/specifications/{subcategoryID?}
与传出(URL 代)匹配完全无关 - 它仅匹配传入的 URLs 并确定 URL 将生成时的样子
如果要使用 Specifications
而不是 Specification
作为路由值,则需要将操作方法移动到名为 SpecificationsController
的新控制器。也就是说,我看不出它有什么区别,因为最终用户无论如何都看不到路由值名称。