C# API Routes (Beginner)

Asked

Viewed 249 times

0

I created a new project like: ASP.NET Web Application - WEB API

I created a new controller, however, I’m not being able to differentiate the ACTIONS.

If I have more than one method of type GET, at the time I request I get the error:

"Multiple actions were found that match the request: \r\nIndex on type DGBar.Controllers.TestController\r\nTeste on type DGBar.Controllers.TestController"

The configured route is the default:

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

I believe the error is in the route, where it was not configured the address with "api" but I could not make the necessary adjustments to make it work.

I am ordering "https://localhost:44398/api/Test/Test" but the TEST action is being completely ignored.

Could someone help?

Thank you. https://github.com/zecaloteiro/DGBar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace DGBar.Controllers {
  public class TestController : ApiController {
    [HttpGet]
    public IHttpActionResult Index() {
      return Json("Index");
    }
    [HttpGet]
    public IHttpActionResult Teste() {
      return Json("Teste");
    }
  }
}
  • can post the methods you have in your controller as well?

  • Opa, of course. I edited the post. Thank you very much for the strength.

2 answers

0

You can create your own routes as follows.

namespace DGBar.Controllers {
  public class TestController : ApiController {
    [HttpGet]
    public IHttpActionResult Index() {
      return Json("Index");
    }
    [HttpGet("teste")]
    public IHttpActionResult Teste() {
      return Json("Teste");
    }
  }
}

Their error occurs because they have two methods with the verb GET without differentiation between them.

  • Thanks for the feedback. But if I use the notation you suggested, I get error: 'Httpgetattribute' does not contain a constructor that takes 1 Arguments

  • I believe that my concept of REST API is a little mistaken because after researching I believe that my structure should have only call types (get, post, put, delete, etc) inside the controller and not need to create {control}/{action}

0


You can do it like this:

In your settings, the default will be:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}"
        );

In your controller, you should use two nameplates:

 [RoutePrefix("test")]
    public class TestController : ApiController
    {    
        [HttpGet]
        [Route("index")]
        public string Index(string No)
        {
            try
            {
                return No;
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        [HttpGet]
        [Route("teste")]
        public string Teste(string No)
        {
            try
            {
                return No;
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }
    }

There to consume use:

endereco/api/test/index or endereco/api/test/teste

You can read more about in the official documentation: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

  • Thanks. It worked. Route("") notation replaces . net Core’s [Httpget("test")].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.