Year End Sale: Get Upto 40% OFF on Live 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

05 Dec 2024
Intermediate
117K Views
4 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

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.

Read More: Rest API Interview Questions and Answers for Experienced
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)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this