【发布时间】:2018-11-04 22:23:18
【问题描述】:
我正在使用 MVC 5,我正在尝试通过 System.Web.Mvc.RouteAttribute 完成路由。
因此,大多数操作都有效,而一个则无效。
我创建了一个Delete-action 和一个Edit-action。两者看起来一样。
这里是Delete-方法:
[Route("data/links/delete/{id}")]
public async Task<ActionResult> Delete(int? id)
{
// ....
return View(link);
}
// in the view, DeleteConfirmed is called on submit
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Route("data/links/delete/{id}")]
public async Task<ActionResult> DeleteConfirmed(int id)
{
// ....
return RedirectToAction("Index");
}
这里是Edit-方法:
[Route("data/links/edit/{id}")]
public async Task<ActionResult> Edit(int? id)
{
// ....
return View(link);
}
[HttpPost]
[ValidateAntiForgeryToken]
[Route("data/links/edit")]
public async Task<ActionResult> Edit([Bind(Include = "Id,LinkText,Url,Image,Description")] Link link)
{
// ....
return this.RedirectToAction("Index");
}
所以,它们的路由看起来是一样的。
我称之为GET-methods 的链接也一样:
@Html.ActionLink("Bearbeiten", "Edit", new { id = item.Id })
@Html.ActionLink("Löschen", "Delete", new { id = item.Id })
有趣的是:
the link to edit gets rendered: http://localhost:45132/data/links/edit?id=2
the link to delete gets rendered: http://localhost:45132/data/links/delete/2
为什么编辑会渲染到edit?id=2,而删除会渲染到delete/2?
编辑链接不起作用。当我在http://localhost:45132/data/links/edit/2 手动输入编辑页面时,链接有效。 但是 ActionLink 给我一个错误的 URL。有什么想法吗?
更新
我的 RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
【问题讨论】:
-
你
RouteConfig.cs的内容是什么? -
@SeM 添加了 RouteConfig.cs
-
@MatthiasBurger 是的,没仔细看。也许它正在从 post 方法中获取路线。
标签: c# asp.net-mvc asp.net-mvc-5 routing