1

I have the following route to my method:

[HttpGet]
[Route("Company/Employees/{name}")]
public ActionResult Details(string name)

I want to access the employee with a name property of "John" by making a request to

Company/Employees/John

But now the route only works if I type:

Company/Employees?name=John

How can I fix this?

Edit:

This is my route config (inside an area)

context.MapRoute(
            "Company_default",
            "Company/{controller}/{action}/{id}",
            new { controller = "Employees", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "MySite.Areas.Company.Controllers" }
        );
11
  • Have you tried replacing the {name} part with {id} ? Commented Jan 29, 2016 at 8:28
  • @RosdiKasim - yeap, not working Commented Jan 29, 2016 at 8:31
  • It doesn't seem wrong to me, was there another actionresult that uses the same namespace? Commented Jan 29, 2016 at 8:32
  • @aeee98 - there are other methods in the same controller Commented Jan 29, 2016 at 8:35
  • Try changing both to id in the route and in the parameter variable name Commented Jan 29, 2016 at 8:39

2 Answers 2

2

As I already say, your default route have same sturture as your custom Contoller Route. Becouse {id} param is optional.

If you want to use your route you should define custom route in your RouteConfig BEFORE your default route and get rid of your Route Attribute on controller:

context.MapRoute(
            "DetailsCustom",
            "Company/Employees/{name}",
            new { controller = "Employees", action = "Details"},
            namespaces: new[] { "MySite.Areas.Company.Controllers" }
        );
Sign up to request clarification or add additional context in comments.

Comments

0

Add this before your other route:

context.MapRoute(
        "Company_default",
        "Company/{controller}/{id}",
        new { controller = "Employees", action = "Index", id =    UrlParameter.Optional },
        namespaces: new[] { "MySite.Areas.Company.Controllers" }
    );

as your previous one is picking up "John" as the "action" parameter

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.