I'm using MVC 5 and I'm trying to get the routing done via System.Web.Mvc.RouteAttribute.
So the most actions do work, one doesn't.
I created a Delete-action and an Edit-action. Both look the same.
Here the Delete-method:
[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");
}
Here the Edit-method:
[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");
}
So, the routing of them both looks the same.
The links I call them GET-methods are the same too:
@Html.ActionLink("Bearbeiten", "Edit", new { id = item.Id })
@Html.ActionLink("Löschen", "Delete", new { id = item.Id })
Funny thing is:
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
Why is edit rendered to edit?id=2 and delete to delete/2?
The edit-link doesn't work. When I manually enter the edit-page at http://localhost:45132/data/links/edit/2 then the link works. But ActionLink gets me a wrong URL. Any idea?
update
My 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?