Skip to content

How to Migrate Existing Web API Controllers by Using a Compatibility Shim

.NET and Microsoft decided to merge the MVC and Web API functionality, after the release of ASP.NET. If you are looking for the implementation of Web API with ASP.NET, Here is the way to accomplish:

Migrating Existing Web API Controllers by Compatibility Shim

You will have to alter one code to call AddWebApiConventions() if you use HttpResponseMessage in Web API controllers. It will help to know the proper serialize that response by wiring up the HttpResponseMessageOutputFormatter.

public void ConfigureServices(IServiceCollection services)
	// Add framework services.
	services.AddMvc().AddWebApiConventions(); //Add WebApi

}Depending upon your routing with Web API. You will need to register routes with Startup class.

In this way, you can migrate from your Web API code to ASP.NET Core. 

public void Configure(IApplicationBuilder app, 
                IHostingEnvironment env, ILoggerFactory loggerFactory)
    app.UseMvc(routes =>
	routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");

This sample controller helps you to use your ApiController within ASP.NET Core with compatibility Shim.

public class ValuesApiController : ApiController
	public IEnumerable GetArray()
		//example returns a basic type that is automatically serialized as json - nothing special here
		return new string[] { "value2", "value3" };

	public HttpResponseMessage GetSomeJson()
		//example returns a HttpResponseMessage - the shim makes this possible
		var resp = new HttpResponseMessage()
			Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]")

		resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
		return resp;



Deependra Kushwah View All

Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

What you think

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: