0

i would like to create rest api but I have problem with routing. This url is ok: localhost/project/controller/action/id, but i would like this url localhost/project/controller/id too. For example: localhost/project/article/5: ArticleController.Get(int id) This is my code: Project controller

[HttpGet]
[Route("project/{id}")]
public JsonResult Get(int id)
{
}

Route config:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Api",
                url: "{controller}/{id}",
                defaults: new { id = UrlParameter.Optional }
            );
        }

When i call url localhost/project/article/5, error message is A public action method '5' was not found on controller 'ArticleController'.

Where is problem? Thanks

1
  • Which version of mvc are you using? Commented May 9, 2017 at 11:09

1 Answer 1

1

You can use attribute routing

Example:

[HttpGet]
[Route("project/{controller}/{action}/{id}")]
public JsonResult Get(int id)
{
}

OR

Default Route Table

Example:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();
            routes.MapRoute(
                name: "Api",
                url: "project/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }

As Per your Requirement, your Routing is:

[HttpGet]
[Route("project/article/{id:int}")]
public JsonResult Get(int id)
{
}

OR

  routes.MapRoute(
                    name: "Api",
                    url: "project/article/{id}",
                    defaults: new { controller = "ArticleController", action = "Get", id = UrlParameter.Optional }
                );
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but this dont work correctly. I need map url localhost/project/article/5 to ArticleController.Get(int id)
I am sorry, it doesnt work correctly. Error message is A public action method '5' was not found on controller
I created simple example ApiController: ` [HttpGet] [Route("api/example")] public void Get(int id) { }` When i call url api/example/1 return 404
No , you try like [HttpGet] [Route("api/example/{id:int}")] public void Get(int id) { } @bluray
@bluray. sorry for my mistake I update "/project/article/{id:int}") to "project/article/{id:int}")
|

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.