I am trying to work through some of the excellent examples found in Dan Roth's great Deep Dive video to create my first Web API and begin implementation of a new project using the RC. I downloaded and installed the standalone installer for VS2010 and found
the new MVC 4 project type in the list, and I was able to create a new MVC 4 project using the WebAPI template.
I tried writing a simple API controller that created a new contact using a POST request and returned the new contact the new location in the header using the following code (translated loosely to VB from Dan's excellent C# code in the video):
Public Function Post(ByVal contact As ContactThing) As HttpResponseMessage
' do something to save the new contactthing here
' construct the response
Dim response as HttpResponseMessage = Request.CreateResponse(HttpStatusCode.Created, contact)
response.Headers.Location = new Uri(Url.Link("DefaultApi", new with {.ID = contact.ID }))
return response
End Function
Unfortunately, I have no version of the Request property or object exposed in my ApiController's Post method that contains a CreateResponse method or any method starting with Create*. Nor do I have the Nice Uri helper to help build that Location header.
Have I missed an installation step or something here? I am driving myself nuts trying to find some of the methods and properties discussed online in various blog posts and articles, but they just ain't there? Do I need to be coding using the VS2012 RC with
the 4.5 framework to see these options?
I am coding in VS 2010 SP1 for the .NET Framework 4.0 (and feverishly translating from C# to VB -- any of you guys ever write in VB anymore??
I have no choice, dedicated VB shop) after installing the RC for MVC 4 using the standalone installer. I've seen mentions
of using the nightly builds, but Dan's solution in the video indicated that his code was using the RC build.
Thanks for ANY suggestions you might have on how I can find and use these method calls and properties.
Thanks, Mike. I'll look and see if I can find that assembly. I guess I assumed that a reference to it would have been added by the project template since the video didn't mention needing to add any references to use the method.
They are both in System.Web.Http.dll. It should be in the project already. I was wondering if you are missing an "Imports" statement for the namespaces?
So I did a search for CreateResponse in the object browser and came up with 9 matches. Clicking on the last two show me that they are members of System.Net.Http.HttpRequestMessageExtensions, and clicking on HttpRequestMessageExtensions shows that it is
in namespace System.Net.Http in the assembly System.Net.Http.Formatting. The method signatures of these 2 are not what I am expecting, and they do not take a Type parameter - no generic CreateResponse(Of T).
The other 7 all have a Type parameter - CreateResponse(Of T) and accept the parameters that I am expecting to see (StatusCode, object of type T, ...), but when I click on one of these 7 I see no information in either of the other 2 panes of the Object Browser.
It's like the IDE knows they are there, but it doesn't know where to locate them.
Strange behavior. I must have some sort of installation issue. The assemblies are in my packages folder under my project. Should they be in the GAC??
So I have managed to get the code to compile and work. However, I'm just not getting any intellisense for the Request Message extensions when editing my ApiController. That's OK for now, 'cause I can guess what the method call needs to look like for now.
Hopefully this will be resolved in the production code.
richmiller
Member
40 Points
51 Posts
HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 05:22 PM|LINK
Hi, All.
I am trying to work through some of the excellent examples found in Dan Roth's great Deep Dive video to create my first Web API and begin implementation of a new project using the RC. I downloaded and installed the standalone installer for VS2010 and found the new MVC 4 project type in the list, and I was able to create a new MVC 4 project using the WebAPI template.
I tried writing a simple API controller that created a new contact using a POST request and returned the new contact the new location in the header using the following code (translated loosely to VB from Dan's excellent C# code in the video):
Public Function Post(ByVal contact As ContactThing) As HttpResponseMessage ' do something to save the new contactthing here ' construct the response Dim response as HttpResponseMessage = Request.CreateResponse(HttpStatusCode.Created, contact) response.Headers.Location = new Uri(Url.Link("DefaultApi", new with {.ID = contact.ID })) return response End FunctionUnfortunately, I have no version of the Request property or object exposed in my ApiController's Post method that contains a CreateResponse method or any method starting with Create*. Nor do I have the Nice Uri helper to help build that Location header. Have I missed an installation step or something here? I am driving myself nuts trying to find some of the methods and properties discussed online in various blog posts and articles, but they just ain't there? Do I need to be coding using the VS2012 RC with the 4.5 framework to see these options?
I am coding in VS 2010 SP1 for the .NET Framework 4.0 (and feverishly translating from C# to VB -- any of you guys ever write in VB anymore??
I have no choice, dedicated VB shop) after installing the RC for MVC 4 using the standalone installer. I've seen mentions
of using the nightly builds, but Dan's solution in the video indicated that his code was using the RC build.
Thanks for ANY suggestions you might have on how I can find and use these method calls and properties.
Rich
MikeWasson
Member
486 Points
78 Posts
Microsoft
Re: HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 05:40 PM|LINK
CreateResponse is an extension method defined in System.Net.Http.HttpRequestMessageExtensions.
The Link method (System.Web.Http.Routing.UrlHelper.Link) is available from the ApiController.Url property.
- Mike
richmiller
Member
40 Points
51 Posts
Re: HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 05:48 PM|LINK
Thanks, Mike. I'll look and see if I can find that assembly. I guess I assumed that a reference to it would have been added by the project template since the video didn't mention needing to add any references to use the method.
MikeWasson
Member
486 Points
78 Posts
Microsoft
Re: HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 06:07 PM|LINK
They are both in System.Web.Http.dll. It should be in the project already. I was wondering if you are missing an "Imports" statement for the namespaces?
- Mike
richmiller
Member
40 Points
51 Posts
Re: HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 06:57 PM|LINK
There are my Imports:
I am seeing the Link method on the Url helper. Just don't see CreateRequest on the Request object.
Rich
richmiller
Member
40 Points
51 Posts
Re: HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 07:15 PM|LINK
So I did a search for CreateResponse in the object browser and came up with 9 matches. Clicking on the last two show me that they are members of System.Net.Http.HttpRequestMessageExtensions, and clicking on HttpRequestMessageExtensions shows that it is in namespace System.Net.Http in the assembly System.Net.Http.Formatting. The method signatures of these 2 are not what I am expecting, and they do not take a Type parameter - no generic CreateResponse(Of T).
The other 7 all have a Type parameter - CreateResponse(Of T) and accept the parameters that I am expecting to see (StatusCode, object of type T, ...), but when I click on one of these 7 I see no information in either of the other 2 panes of the Object Browser. It's like the IDE knows they are there, but it doesn't know where to locate them.
Strange behavior. I must have some sort of installation issue. The assemblies are in my packages folder under my project. Should they be in the GAC??
richmiller
Member
40 Points
51 Posts
Re: HttpResponseMessage and APIController.Request Object with CreateResponse Method: Where??
Jul 20, 2012 08:11 PM|LINK
So I have managed to get the code to compile and work. However, I'm just not getting any intellisense for the Request Message extensions when editing my ApiController. That's OK for now, 'cause I can guess what the method call needs to look like for now. Hopefully this will be resolved in the production code.
Thanks for the help.
Rich