Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default route in asp.net web api

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

localhost:45678/api/Products

But i didnt find any format similar to asp.net mvc route model like

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

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

like image 968
Karthik Bammidi Avatar asked Jun 23 '12 19:06

Karthik Bammidi


1 Answers

Check your your RouteConfig class in your App_Start folder. You should see the default route below 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 want to do it, but you just need to specify the Start URL for your project. Right click your web project - click Properties - click the Web tab - under Start Action select Start URL and enter http://localhost:45678/api/Products in the box. Save your project and run again. It should start at the new location.

like image 76
Kevin Aenmey Avatar answered Oct 21 '22 03:10

Kevin Aenmey