
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?
The DefaultHttpControllerSelector class in web api is responsible for selecting the appropriate controller action method that we send in the URI.
Say we have to implement versioning in the query string like below
v=1 StudentsV1Controller (Version 1) v=2 StudentsV2Controller (Version 2)
If we pass the versioning information in the query string like http://localhost:58174/api/student?v=1 will result in 404 Not Found error response because the SelectController() method which is present in DefaultHttpControllerSelector will look for the StudentsController but we have only StudentsV1Controller and StudentsV2Controller.
To handle this case, we should add our own CustomControllerSelector which implements the DefaultHttpControllerSelector class.
CustomControllerSelector −
Example
using System.Net.Http; using System.Web; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace WebAPI.Custom{ public class CustomControllerSelector : DefaultHttpControllerSelector{ private HttpConfiguration _config; public CustomControllerSelector(HttpConfiguration config) : base(config){ _config = config; } public override HttpControllerDescriptor SelectController(HttpRequestMessage request){ var controllers = GetControllerMapping(); var routeData = request.GetRouteData(); var controllerName = routeData.Values["controller"].ToString(); string versionNumber = "1"; var versionQueryString = HttpUtility.ParseQueryString(request.RequestUri.Query); if (versionQueryString["v"] != null){ versionNumber = versionQueryString["v"]; } if (versionNumber == "1"){ controllerName = controllerName + "V1"; } else if (versionNumber == "2"){ controllerName = controllerName + "V2"; } HttpControllerDescriptor controllerDescriptor; if (controllers.TryGetValue(controllerName, out controllerDescriptor)){ return controllerDescriptor; } return null; } } }
The next thing that we need to replace the default controller selector with our custom controller selector. This is done in WebApiConfig.cs file. Notice we are replacing IHttpControllerSelector, with our CustomControllerSelector. DefaultHttpControllerSelector implements IHttpControllerSelector, so that is the reason we are replacing IHttpControllerSelector.
Example
public static class WebApiConfig{ public static void Register(HttpConfiguration config){ config.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(config)); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
StudenV1Controller −
using DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class StudentV1Controller : ApiController{ List<StudentV1> students = new List<StudentV1>{ new StudentV1{ Id = 1, Name = "Mark" }, new StudentV1{ Id = 2, Name = "John" } }; public IEnumerable<StudentV1> Get(){ return students; } public StudentV1 Get(int id){ var studentForId = students.FirstOrDefault(x => x.Id == id); return studentForId; } } }
StudentV2Controller −
using DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class StudentV2Controller : ApiController{ List<StudentV2> students = new List<StudentV2>{ new StudentV2{ Id = 1, FirstName = "Roger", LastName = "Federer" }, new StudentV2{ Id = 2, FirstName = "Tom", LastName = "Bruce" } }; public IEnumerable<StudentV2> Get(){ return students; } public StudentV2 Get(int id){ var studentForId = students.FirstOrDefault(x => x.Id == id); return studentForId; } } }
Output
Below outputs shows the results that we get from StudentV1 and StudentV2 controllers with versioning in query string.
- Related Articles
- How to do versioning with accept header in C# ASP.NET WebAPI?
- How to do Web API versioning with URI in C# ASP.NET WebAPI?
- How to do versioning with custom media type in C# ASP.NET WebAPI?
- How to use ViewBag in ASP .Net MVC C#?
- What is ViewData in ASP .Net MVC C#?
- What is parameter binding in C# ASP.NET WebAPI?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- How do you compare Python objects with .NET objects?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- How do we specify MIME type in Asp.Net WebAPI C#?
- How to configure Gson to enable versioning support in Java?
- How to validate the path with the PowerShell function parameter?
