Change the "/api" part of the url in ASP.Net Core

David Derman picture David Derman · Feb 23, 2017 · Viewed 9.5k times · Source

When I run a .NET Core Web API project, I get a URL that looks like this: http://localhost:5000/api/...

How can I change the /api/ part of the URL to something else? i.e. - http://localhost:5000/myservice/...

I am using Kestrel as my web host.

Answer

Woot picture Woot · Feb 23, 2017

It depends on how you have the project setup. By default I believe it uses attribute routing. In your controller you should see something like this

[Route("api/[controller]")]
public class ValuesController : Controller
{

Where the [Route("api/[controller]")] would just need to be changed to [Route("myservice/[controller]")]

If you wanted to do it Globally you could do it like this.

app.UseMvc(routes =>
{
   routes.MapRoute("default",  "myservice/{controller=values}/{action=get}/{id?}");
});

Although I myself don't use this, and it's not exactly what MS Recommends for a Web Api. You can read more here.

Mixed Routing MVC applications can mix the use of conventional routing and attribute routing. It's typical to use conventional routes for controllers serving HTML pages for browsers, and attribute routing for controllers serving REST APIs.