4

This issue might be raised earlier hence can be duplicate.

When we configure ASP.NET core app (MVC) we define and map routes under Startup.cs - Configure - app.useMvc(). And this works perfectly. However, when WebAPI comes into picture, why we need to specify [Route("api/[controller]") specifically? In other way, webAPI only works with attribute routing?

Why can't Configure method handles this route configuration? Which was good option in earlier MVC/WebAPI (not core).

Am I missing anything?

2
  • are you using the mvc &web api together in application or different application? Commented Feb 8, 2017 at 17:27
  • I have complete stand alone webapi project. Would like to make routing configuration thru startup.cs instead of each controller level Commented Feb 8, 2017 at 17:32

2 Answers 2

4

In ASP.NET Core MVC, there is no distinction between MVC and Web API (like there was in MVC 5 / Web API 2). The routing should work identically regardless of whether the request is getting back a View or another kind of ActionResult. That said, attribute routing is the default convention in ASP.NET Core MVC, since it puts the routing logic closer to the code that will respond to the route. You can still set up global routes in Startup.Configure (as you've seen), but the conventional, recommended approach is to use attributes at the controller and/or action level.

If you're seeing behavior in which a particular (web api) action isn't using a route you've set up globally, please post the code (perhaps in another question) as that doesn't sound like the expected behavior to me.

Sign up to request clarification or add additional context in comments.

4 Comments

"That said, attribute routing is the default convention in ASP.NET Core MVC, since it puts the routing logic closer to the code that will respond to the route.", if this said by ASP.NET Core team, can I have reference of this for my use.
"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." from learn.microsoft.com/en-us/aspnet/core/mvc/controllers/… is about as close as I can get to that.
Though, it is pain if we are coming from asp.net mvc-webapi background, where both scenarios working correctly, global route config and attribute routing.
hi ssmith, can you take a look at this question stackoverflow.com/questions/45679478/… - using attribute routing and does not seem to work when i change the body of a method.
0

The Web API routing is very similar to MVC routing. In the Web API routing, we can find "api/" at the beginning of the URL which makes it distinct from MVC Routing. In Web API routing "action" parameters is not mandatory but it can be included in a part of routing.

API Routing:

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

MVC Routing:

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Library", action = "Index", id = UrlParameter.Optional }
        );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.