0

Error Message:

Action 'SampleCusAppAPI.Controllers.CustomerController.GetCustomer (SampleCusAppAPI)' does not have an attribute route. Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.

Code: StartUp.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();



            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();  // Error Occured in this line.
            });


        }

Web Api code: CustomerController

namespace SampleCusAppAPI.Controllers
{
    [ApiController]
    //[System.Web.Http.Route("api/[controller]")]
    public class CustomerController : ControllerBase
    {
        SampleTestingDBContext objSampleTestingDBContext = new SampleTestingDBContext();
        [System.Web.Http.Route("api/Customer/GetCustomer")]        
        [System.Web.Http.HttpGet]
        public IQueryable<Customer> GetCustomer()
        {
            return objSampleTestingDBContext.Customers.OrderByDescending(x => x.CustomerId);
        }
}
}
8
  • the error states clearly about this GetCustomer(SampleCusAppAPI), where is that method? it should be decorated with route attribute Commented Feb 23, 2021 at 6:38
  • @king king, Can you send example / Link? Commented Feb 23, 2021 at 7:10
  • the example lies right in what you posted, but the code is for GetCustomer(), not for GetCustomer(SampleCusAppAPI), so that's why I asked where that method is. Commented Feb 23, 2021 at 7:12
  • @king king, I have only one method in my api controller getCustomer(). But error occured like this(GetCustomer(SampleCusAppAPI)). This error occured in app.UseEndpoints. Commented Feb 23, 2021 at 7:19
  • @king king Please send me any web api routing example link. I am new to core. Commented Feb 23, 2021 at 7:21

1 Answer 1

1

This is the example of a GET api that you want. It might help you resolve the problem.

/// <summary>
    /// Get task by id
    /// GET : api/tasks/{id}
    /// </summary>
    /// <param name="task_id"></param>
    /// <returns></returns>        
    [HttpGet("{task_id:long}")]
    public async Task<IActionResult> Get(long task_id)
    {
        var task = await _taskService.GetTaskByIdAsync(task_id, _user.org_id);
        if (task != null)
        {
            var assignee = await _taskService.GetTaskAssigneeAsync(new long[] { task_id });
            task.assignee = assignee;
            return Ok(task);
        }
        return NotFound(Logger.Error($"No task found with id {task_id}", HttpStatusCode.NotFound));
    }
      
Sign up to request clarification or add additional context in comments.

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.