Financial Year 2023 End Sale: Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Comparing Asp.Net Web API Routing and Asp.Net MVC Routing

Comparing Asp.Net Web API Routing and Asp.Net MVC Routing

04 Sep 2022
Intermediate
115K Views
4 min read

As you know, Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. A URL pattern is matched against the routes patterns defined in the Route dictionary in an Order and the first match wins. This means the first route which successfully matches a controller, action, and action parameters defined in the URL will call into the specified controller and action.

Asp.Net MVC application and Asp.Net Web API must have at least one route defined in order to function. Hence, Visual Studio templates defined for MVC and Web API must have a default route. Now let's understand the difference between Asp.Net MVC and Asp.Net Web API.

Default Route Pattern

The default route pattern for a Web API Project is defined as follows-

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

The literal api at the beginning of the Web API route pattern, makes it distinct from the standard MVC route. This is not mandatory but it is a good convention to differ Web API route from MVC route.

In Web API route pattern {action} parameter is optional but you can include an {action} parameter. Also, the action methods defined on the controller must be have an HTTP action verb as a prefix to the method name in order to work. So, you can also define the route for Web API as follows-

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

The default route pattern for an Asp.Net MVC Project is defined as follows-

routes.MapRoute(
 name: "Default", //route name
 url: "{controller}/{action}/{id}", //route pattern
 defaults: new 
 { 
 controller = "Home", 
 action = "Index", 
 id = UrlParameter.Optional
 } //parameter default values
 );

As you have seen there is no literal before at the beginning of the Asp.Net MVC route pattern but you can add if you wish.

Route Processing

In Web API route processing the URLs map to a controller, and then to the action which matches the HTTP verb of the request and the most parameters of the request is selected. The Action methods defined in the API controller must either have the HTTP action verbs (GET, POST, PUT, DELETE) or have one of the HTTP action verbs as a prefix for the Actions methods name as given below-

public class ValuesController : ApiController
{
 // GET api/
 public IEnumerable Get()
 {
 return new string[] { "value1", "value2" };
 }

 // GET api//5
 public string Get(int id)
 {
 return "value";
 }

 // POST api/
 public void Post([FromBody]string value)
 {
 }

 // PUT api//5
 public void Put(int id, [FromBody]string value)
 {
 }

 // DELETE api//5
 public void Delete(int id)
 {
 }
} 

// OR You can also defined above API Controller as Verb Prefix

public class ValuesController : ApiController
{
 // GET api/values
 public IEnumerable GetValues()
 {
 return new string[] { "value1", "value2" };
 }
 // GET api/values/5
 public string GetValues(int id)
 {
 return "value";
 }
 // POST api/values
 public void PostValues([FromBody]string value)
 {
 }
 // PUT api/values/5
 public void PutValues(int id, [FromBody]string value)
 {
 }
 // DELETE api/values/5
 public void DeleteValues(int id)
 {
 }
}

In Asp.Net MVC route processing the URLs map to a controller, and then to the action which matches the HTTP verb of the request and the most parameters of the request is selected. The Action methods defined in the MVC controller do not have HTTP action verbs as a prefix for the Actions methods but they have name like as normal method as shown below-

public class HomeController : Controller
{
 // GET: /Home/Index
 public ActionResult Index() //method - Index
 {
 // To Do:
 return View();
 }

 // Post: /Home/Index
 [HttpPost]
 public ActionResult Index(LoginModel model, string id)
 {
 // To Do:
 return View();
 }
} 

In MVC, by default HTTP verb is GET for using others HTTP verbs you need defined as an attribute but in Web API you need to define as an method's name prefix.

Complex Parameter Processing

Unlike MVC, URLs in Web API cannot contain complex types. Complex types must be placed in the HTTP message body and there should be only one complex type in the body of an HTTP message.

Base Library

Web API controllers inherit from, but MVC controllers inherit from System.Web.Mvc.Controller. Both the library are different but acts in similar fashion.

What do you think?

I hope you will enjoy the tips while programming with Asp.Net MVC and Web API. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this