2

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 }
    );
}
3
  • What is the content of your RouteConfig.cs? Commented May 25, 2018 at 13:42
  • @SeM added RouteConfig.cs Commented May 25, 2018 at 13:43
  • @MatthiasBurger Yeah, Did not look at it careful enough. Maybe it is picking up the route from the post method. Commented May 25, 2018 at 13:48

2 Answers 2

1

I would suggest you to use RouteLink helper method instead.

Define Name property to your Route attribute, for example:

[Route("data/links/edit/{id}", Name = "MyRoute")]

then use:

@Html.RouteLink("Bearbeiten", "MyRoute", new { id = item.Id })
Sign up to request clarification or add additional context in comments.

Comments

1

It is behaving as designed. You have a complex model in the edit, which is also not part of the route template. so it has to put the properties provided when generating the link in the query string.

To get the desired routes...

[HttpGet]
[Route("data/links/edit/{id:int}")] // GET data/links/edit/2
public async Task<ActionResult> Edit(int id) {
    // ....
    return View(link);
}

[HttpPost]
[ValidateAntiForgeryToken]
[Route("data/links/edit/{id:int}")] // POST data/links/edit/2
public async Task<ActionResult> Edit(int id, [Bind(Include = "Id,LinkText,Url,Image,Description")] Link link) {
    // ....
    return this.RedirectToAction("Index");
}

1 Comment

Ah I need to add the id as parameter in the POST that the GET is called...?! I see... I stick with SeMs answer. Looks cleaner to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.