1

My controller code looks like this:

[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{        
    [HttpGet]
    public async Task<string> GetPaymentDetail()
    {
        return "Success";
    }
}

I am trying to access like this

http://localhost:47744/api/paymentdetail
http://localhost:47744/api/paymentdetail/GetPaymentDetail

I cant access my controller and method

1
  • paymentdetail is the name of the controller - GetPaymentDetail is the action method - which gets combined to a complete URL of /api/paymentdetail/GetPaymentDetail - as desired - so what's the issue or question, really??? Commented Jun 20, 2021 at 19:29

4 Answers 4

3

if you want to use route like this

http://localhost:47744/api/paymentdetail/GetPaymentDetail

you need this controller route

[Route("api/[controller]/[action]")]
public class PaymentDetailController : ControllerBase
{        
    [HttpGet]
    public async Task<string> GetPaymentDetail()
    {
        return "Success";
    }
}

if you want to use route like this

http://localhost:47744/api/GetPaymentDetail

you need this controller route

[Route("api/[action]")]
public class PaymentDetailController : ControllerBase
{        
    [HttpGet]
    public async Task<string> GetPaymentDetail()
    {
        return "Success";
    }
}

or you can use both routes

[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{      
     [Route("~/api/PaymentDetail/GetPaymentDetail")]  
     [Route("~/api/GetPaymentDetail")]
    public async Task<string> GetPaymentDetail()
    {
        return "Success";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2
[Route("api/[controller]")]
[ApiController]
public class PaymentDetailController : ControllerBase
{   
    [HttpGet]
    [Route("GetPaymentDetail")]
    public async Task<string> GetPaymentDetail()
    {
        return "Success";
    }
}

Like this?

2 Comments

Since the method name is already GetPaymentDetail, I don't see how this would help in any way.... the URL will be exactly the same ...
Thanks for helping me, but this line of code doesn't work for me.
1

It can be accessed using http://localhost:47744/api/paymentdetail, and the route will look for the action of the first get method. enter image description here

Or like this:

[Route("api/[controller]")]
    [ApiController]
    public class PaymentDetailController : ControllerBase
    { 

      [HttpGet("GetPaymentDetail")]

        public async Task<string> GetPaymentDetail()
        {
            return "Success";
        }
      

1 Comment

please can you share startup.cs class code
0

Your URL points to /api/paymentdetail but your ActionMethod is called GetPaymentDetail().

The Url that should be working would most likely be /api/paymentdetail/getpaymentdetail.

You might want to rename your ActionMethod to Get() since it is already clear what resource you are getting.

2 Comments

paymentdetail is the name of the controller - GetPaymentDetail is the action method - which gets combined to a complete URL of /api/paymentdetail/GetPaymentDetail - as desired ..
Overlooked the second URL in the original post. You are right. The second URL should be working.

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.