i wanted to change the launching url of the WebAPI from https://localhost:7027/controller
to https://localhost:7027/test/controller
. i have tried adding app.UsePathBase("/test")
but it only routes swagger. i tried it with postman and it says 404 not found. how do i go about accomplishing this please. thank you
Asked
Active
Viewed 1,940 times
0

Danny
- 317
- 4
- 17
-
Your controller should inherit the ControllerBase class and have the [ApiController] attribute and optionally you could add a [Route] attribute to specify that controller's route. For example [Route("test/[controller")]. So if you create an action GetMyName and add a Route attribute [Route("getMyName")] in a Controller with a name MyController it will have url /test/MyController/getMyName. You should have tha app.MapControllers in your Program.cs. Please edit the post and add some of your Program.cs and controller's code, so that we can see how you are configuring the application. – AchoVasilev Jun 22 '22 at 04:41
1 Answers
0
You can do that with attribute routing for a specific controller or
[Route("test/[controller]")]
public class ValuesController : Controller
{
Or globally you can do like below
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
});
See below - Change the "/api" part of the url in ASP.Net Core

Jalpesh Vadgama
- 13,653
- 19
- 72
- 94
-
i tried that.. and i get this error.. System.InvalidOperationException: 'Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...).' – Danny Jun 21 '22 at 12:02
-
I just changed the answer actually after 2.2 version we need to use 'endpoints.MapControllerRoute' – Jalpesh Vadgama Jun 21 '22 at 12:04
-
now i have this error.. System.InvalidOperationException: 'EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code.' – Danny Jun 21 '22 at 12:09
-
-
Yes please add app.UseRouting before app.UseEndPoins as per @Rena's comment – Jalpesh Vadgama Jun 24 '22 at 08:56