Karthik Bammidi
Karthik Bammidi

Reputation: 1851

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.

Upvotes: 13

Views: 40734

Answers (8)

Osama Almostafa
Osama Almostafa

Reputation: 1

You need to add:

Response.Redirect("Products.aspx");

to method index() in the class HomeController.

Upvotes: 0

CasualCoder
CasualCoder

Reputation: 51

In my ASP .NET Core Web API project, whenever you DEBUG the project, it fires up a browser to a predefined URL. This can be configered by right-clicking the project and going to properties and then Debug tab as shown below. enter image description here

That said, not sure what the difference is between this and the launchsettings.json described in a previous answer above.

enter image description here

Upvotes: 0

yibe
yibe

Reputation: 378

While developing in Visiual Studio, you can set it by expand the Properties folder of the project. Then open launchSettings.json file and look for "launchUrl" property in this file

You can change the default launch route for the profile you are working on.

"launchUrl" : "api/products"

Upvotes: 1

ComeIn
ComeIn

Reputation: 1609

I think you have danced around the solution but just missed the mark. Try this:

  1. Make sure you are calling GlobalConfiguration.Configure(WebApiConfig.Register); in your Global.asax.cs.

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

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

  3. In your web project settings make sure you have "Start Url" property set as:

    localhost:45678

Good Luck.

Upvotes: 0

Karthik Bammidi
Karthik Bammidi

Reputation: 1851

Actually if we are in the position to set the default route in properties->web->start location. then what is the need of route tables, custom routes,RegisterRoutes in global.asax file. I tried for this way

at first it seems like,

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

Now i want to make localhost:xxxx/api/products as 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 of no use.

Upvotes: 2

Mike Wasson
Mike Wasson

Reputation: 6622

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

Are you saying that when you run the project from Visual Studio, it opens a browser to the project home page?

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

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

When you run the project in Visual Studio, it will navigate to "http://localhost:xxxx" by default. In normal operation, a client would request whichever URI it wanted.

You can change the Visual Studio settings under Project Properties / Web / Start Action.

Upvotes: 0

Kevin Aenmey
Kevin Aenmey

Reputation: 13419

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.

Upvotes: 12

tugberk
tugberk

Reputation: 58434

The issue might be the common mistake that nearly lots of people get into.

The fact here is that the all routes get collected under System.Web.Routing.RouteTable.Routes collection regardless of the framework you use. So, if you put the ASP.NET MVC default route before the ASP.NET Web API route, the ASP.NET Web API route will never be scanned because the MVC route will be a match.

I am assuming this is the case here by looking at what you've provided so far. If that's not the case, upload the full solution out there somewhere and people can have a full look.

Upvotes: 4

Related Questions