How to change default route in asp.net web api

I am working on an asp.net web api. I am trying to set the default route for my project in global.asax file, for example,

localhost:45678/api/Products 

But I did not find a format similar to the asp.net mvc route model, for example

 url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 

It always redirects me to the main page (HomeController). please guide me.

+9
asp.net-mvc asp.net-web-api
source share
7 answers

Check your RouteConfig class in the App_Start folder. You should see the default route under which you can change.

  routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

EDIT

After reading your comments, I think the problem is not with your routes. I'm not sure why you would like to do this, but you just need to provide a start URL for your project. Right-click your web project - click Properties - go to the Web tab - in the "Action" section, select the Start URL and enter http://localhost:45678/api/Products in the field. Save the project and run it again. It should start from a new place.

+9
source share

A problem can be a common mistake that almost many people face.

The fact is that all routes are collected in the System.Web.Routing.RouteTable.Routes collection, regardless of the structure you use. Thus, if you place the default ASP.NET MVC route to the ASP.NET web API route, the ASP.NET web API route will never be checked, as the MVC route will match.

I assume this is the case here, looking at what you have provided so far. If this is not the case, download the complete solution somewhere, and people can get a complete overview.

+4
source share

In fact, if we are able to set the default route in properties-> web-> starting location. what route tables, user routes, RegisterRoutes in global.asax need. I tried this way

at first it seems

 routes.MapHttpRoute( name: "Default Api", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

Now I want to make localhost:xxxx/api/products as the default route for my web api, then

 routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/Products/{id}", defaults: new { controller = "Products", id = RouteParameter.Optional } ); 

But the results are futile.

+1
source share

[EDIT: My answer is the same as Kevin’s]

You say that when you start a project from Visual Studio, it opens a browser on the project home page?

The Web API project template contains an MVC controller plus a Web API controller.

The URI "http: // localhost: xxxx /" goes to the MVC controller, and "http: // localhost: xxx / api / products" goes to the API controller.

When you run a project in Visual Studio, it defaults to "http: // localhost: xxxx". During normal operation, the client asks for whatever URI it wants.

You can change Visual Studio settings in the "Project Properties / Web / Action" section.

0
source share

I think you danced around the solution, but you just didn't notice the mark. Try the following:

  • Make sure you call GlobalConfiguration.Configure (WebApiConfig.Register); in your Global.asax.cs file.

  • In WebApiConfig.Register() Set the default route as:

    routes.MapHttpRoute (name: "DefaultApi", routeTemplate: "api / {Controller} / {id}", default: new {controller = "Products", id = RouteParameter.Optional});

  • In the settings of your web project, make sure that you have the property "Start Url":

    local: 45678

Good luck.

0
source share

Restarting VS as administrator resolved this issue for me. Environmental Information: - OS: Windows 10 (64 bit) - Visual studio: 2017.

0
source share

Under the Visual Studio project, expand the Properties folder. Then open the launchSettings.json file and find the launchUrl property in this file .

You can change the default startup route for the profile you are working with.

"launchUrl": "api / products"

0
source share

All Articles