0
votes

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);
        }
}
}
1
the error states clearly about this GetCustomer(SampleCusAppAPI), where is that method? it should be decorated with route attribute - King King
@king king, Can you send example / Link? - Joel Dharansingh
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. - King King
@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. - Joel Dharansingh
@king king Please send me any web api routing example link. I am new to core. - Joel Dharansingh

1 Answers

1
votes

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));
    }