how do you redirect url to specific endpoints?

Normal MVC controller implements Controller, If you want to create an API controller you have to create a controller which will implement ControllerBase abstract class.

For example an API controller will look like,

[Route("api/[controller]")]
[ApiController]
public class ExampleController : ControllerBase
{
    [HttpGet('getall')]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    .......
}

We can configure the routing from the [Route()] attribute. Here is a convention of naming a controller, here in ‘ExampleController’ will be considered as ‘Example’ for the name of this controller.

So, according to the route attribute( [Route("api/[controller]")] ) the path will be “websiteUrl/api/example”. [controller] tag will match any controller name. So if you rename this controller from ExampleController to TestController(You have to concat “Controller” for controller naming that is how the system will recognize it) the path will be “websieUrl/api/test” for this controller. Otherwise you can hard coded the route as [Route("api/example")], this way it won’t depend on controller name.

for your requirement you can do,

[Route("api/token")]
[ApiController]
public class ExampleController : ControllerBase
{
    // GET: api/token/
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

so, if you run a get request to “websiteUrl/api/token” it will point to this,

[HttpGet]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}

and if want to redirect the url to your desired path, then go to launchsettings.json on your project and if you are using IISExpress then in the json file inside profiles–>IISExpress set “”launchUrl”: “”

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top