1

This is my controller

[Authorize]
[RoutePrefix("service")]
public class ServiceController : BaseController
{
    [HttpGet]
    [Route("~/services")]
    public ActionResult Index();

    [HttpPost]
    [Route]
    public JsonResult Index(int rowCount, string search);

    [HttpGet]
    [Route("new/{subcategoryID}")]
    public ActionResult New(int subcategoryID);

    [HttpGet]
    [Route("edit/{serviceID}")]
    public ActionResult Edit(int serviceID);

    [HttpPost]
    [Route("edit")]
    [ValidateJsonAntiForgeryToken]
    public JsonResult Edit(ServiceJson service);

    [HttpDelete]
    [Route("delete")]
    public ActionResult Delete(int serviceID);
}

When I call

@Url.Action("Edit", "Service", new { serviceID = service.ServiceID})

in my view I get

service/edit?serviceID=12

instead of

service/edit/12

Why is this happening? There is no other GET action on this controller that is named Edit. This is driving me crazy for some time now.

The attribute routing should scan the controllers and do the automapping of routes. Why is it falling back to query string if there is obviously an action with this parameter?

I would also like to note that if I manually enter address

service/edit/12

i will get redirected to the appropriate page.

2 Answers 2

1

You should give names to your routes. E.g.:

[Route("edit/{serviceID}", Name = "EditService")]
public ActionResult Edit(int serviceID)

Then generate route url

@Url.RouteUrl("EditService", new { serviceID = service.ServiceID })

You will get

service/edit/12

Don't forget to map attribute routes in your RouteConfig just before you map default route:

routes.MapMvcAttributeRoutes();
Sign up to request clarification or add additional context in comments.

2 Comments

I do call routes.MapMvcttributeRoutes() and that's it! I do not have any default default route mapping nor do I know what that is!
@Rob so after naming routes you still have id passed in query string?
0

So after alot of tries and errors I fixed the issue although I have absolutely no idea why it got fixed.

The issue is this action

[HttpPost]
[Route("edit")]
[ValidateJsonAntiForgeryToken]
public JsonResult Edit(ServiceJson service);

After I removed

[Route("edit")]

it all started working as expected. This action does not take serviceId. It takes and object(it is expecting JSON). Furthermore it is a HttpPost and not HttpGet so there should be no problem since the request itself is GET not POST.

Why oh why Delilha?!

Comments

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.