How to use WCF Web API in ASP.NET MVC

Topics: Web Api
May 6, 2011 at 1:12 AM

I would like to see some examples of using this Web API in the ASP.NET MVC.  Most examples available use ASP.NET Forms.  Any help on what happend to MVCHostConfiguration? Thanks.

May 6, 2011 at 2:01 PM

Can you be more specific in how you want to use the Web API.

#1 Do you want the Web API and service to be hosted along side the MVC application to be consumed by JQuery in the browser.

#2 Or do you want to have a 'business' layer exposed using Web API to the MVC application. 

I'm going to assume you want #1. I played with it and it seemed to work. This was in my global.asax.

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Ladder", action = "Index", id = UrlParameter.Optional },// Parameter defaults
                new { controller = "^(?!Match).*" } //Need to remove service route
            );

            var config = HttpHostConfiguration.Create()
                                     .SetOperationHandlerFactory(new MatchHttpOperationHandlerFactory(new MediaTypeFormatter[0]));

            RouteTable.Routes.MapServiceRoute<MatchResource>("Match", config);

This was just playing around and I didn't have lots of different things in the pipeline or either the MVC app or the  Service

May 6, 2011 at 11:51 PM
Edited May 6, 2011 at 11:56 PM

Thanks for your information.  I saw Glenn using a custom configuration type inheriting from MvcHostConfuguation and integrating the Web API routing with MVC routing. ( later part of Glenn's demo at MvcConference 2. The demo was based on the Mvc Music Store. )  I am wondering if he dropped the MvcHostConfiguration or renamed.

Coordinator
May 7, 2011 at 12:03 AM

Hi Jay

It sounds like you mean the example that used the DependencyResolver. That was not part of web api rather it was an example of what is possible. I will post updated code for the music store soon. Ping me if you don't see it.

Thanks

Glenn

May 8, 2011 at 2:07 AM

Thanks, Glenn.  I think it would be nice to have at least one sample based on MVC.  There are many developers prefer MVC as a web project template.  Where should I look for the mvc music store sample using the Web Api?

Coordinator
May 8, 2011 at 11:34 PM

OK, we will work on putting it out there. Howard (my co-worker) just came up with a nice custom route constraint that will make it much easier to integrate ASP.NET routes with Service routes. My sample I found out was actually broken. We will work on getting a rev of Music store out there. We need to update it to the latest bits first. Probably be a few weeks.

May 9, 2011 at 5:19 PM
gblock wrote:

OK, we will work on putting it out there. Howard (my co-worker) just came up with a nice custom route constraint that will make it much easier to integrate ASP.NET routes with Service routes. My sample I found out was actually broken. We will work on getting a rev of Music store out there. We need to update it to the latest bits first. Probably be a few weeks.


Hey Glenn,

I've been working on a sample MVC-based solution that contains many aspects of your API and some custom additions that I think will be commonly implemented by many developers.  The solution uses the fork you gave me for exposing IHttpHostConfigurationBuilderAdvanced.

Features include:

  1. A service route constraint that matches by convention.
  2. Custom authentication channel implementing BASIC authentication (based upon Cibrax's blog post).
  3. Custom media formatter based on JSON.NET.
  4. RouteCollection extensions that leverage the service name convention.
  5. HttpContent extension (ReadAsJson<T>).
  6. JsonResponse<T> : HttpResponseMessage<T>.
  7. Consumption of sample services by both HttpClient and jQuery.

I was considering sharing it with the community anyway.  If you are interested in taking a look at it and potentially absorbing it into the project, let me know.

May 9, 2011 at 6:45 PM

Hey David,

Your sample seems like it would be useful to a lot of people.  Why don't you fork http://webapicontrib.codplex.com and add it to the Samples solution there? Then I can pull it into the main project. Or if you want you could send me the source and I'll add it for you.

Darrel

Coordinator
May 9, 2011 at 7:02 PM

Darrel beat me to it. I was going to suggest exactly the same though I was waiting till I got off my bike :-)

May 9, 2011 at 8:21 PM
DarrelMiller wrote:

Hey David,

Your sample seems like it would be useful to a lot of people.  Why don't you fork http://webapicontrib.codplex.com and add it to the Samples solution there? Then I can pull it into the main project. Or if you want you could send me the source and I'll add it for you.

Darrel


Hey Darrel,

I looked at creating a fork and I'm not sure the best way to tie in my stuff.  What would be the best way to send you the bits?

May 9, 2011 at 9:05 PM

You can just email me a zip at darrel at tavis.ca

May 10, 2011 at 2:34 AM

David,

Where can I find the  IHttpHostConfigurationBuilderAdvanced related code.  Is it possible to get that code running on top of the standard version of WCF Web API?  I'm trying to get your sample running based on the Nuget just so that we don't have different builds of the core libraries floating around in the contrib project.

Thanks,

May 10, 2011 at 5:05 PM
DarrelMiller wrote:

You can just email me a zip at darrel at tavis.ca


Hey Darrel,

Thanks for posting the code.  For anyone that's interested, you can download it from the WCF Web API Contrib project here.

Coordinator
May 10, 2011 at 5:20 PM

Sweet! I am going to definitely check it out. I have an upcoming presentation to an internal customer which covers MVC and Web API working together. I might use this :-)

May 10, 2011 at 5:53 PM

Fixed :-)  Thanks David.