Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.Action map a wrong link from Route attribute

Tags:

asp.net-mvc

This is target controller and action:

[RoutePrefix("Editor")]
public class EditorController : Controller

[HttpGet]
    [Route("{id:int}")]
    public ActionResult Edit(int id)

Map method calling:

@Url.Action("Edit", "Editor", new { id = page.Id})

result: /Editor?id=1

required result: /Editor/1

like image 579
Aminion Avatar asked Oct 14 '14 16:10

Aminion


People also ask

What does route attribute do?

As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources. The earlier style of routing, called convention-based routing, is still fully supported.

What maps the URL to controller action with parameters?

The router is responsible for mapping incoming requests to an action method that will be executed to handle the request.

Can we map multiple URLs to the same action with example?

Yes, We can use multiple URLs to the same action with the use of a routing table. foreach(string url in urls)routes. MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });


1 Answers

To achieve the result you want you have to use a route name:

[HttpGet]
[Route("{id:int}", Name = "EditorById")]
public ActionResult Edit(int id)

Then in your view you would use Url.RouteUrl instead of Url.Action:

@Url.RouteUrl("EditorById", new { controller = "Editor", Id = 1, action = "Edit" })

Hope this helps,

like image 178
gsimoes Avatar answered Nov 03 '22 00:11

gsimoes