-
August 31st, 2014, 10:10 PM
#1
Web Api
Hi, I had the following code written in VS2013.
Code:
public EFileInfo UploadFile(string WebApiBaseAddress, string WebApiServiceUrl, WebApiParameters webParams)
{
EFileInfo eFile = null;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(WebApiBaseAddress);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync(WebApiServiceUrl, webParams).Result;
if (response.IsSuccessStatusCode)
{
// Parse the response body.
eFile = response.Content.ReadAsAsync<EFileInfo>().Result;
}
return eFile;
}
How can I amend my code in order to use it on VS2008? Because when I tried to copy the codes to VS2008, it prompt error on HttpClient, MediaTypeWithQualityHeaderValue and HttpResponseMessage declarations saying that missing reference.
-
August 31st, 2014, 10:22 PM
#2
Re: Web Api
Have you checked in msdn online to make sure MediaTypeWithQualityHeaderValue and HttpResponseMessage are supported in .net 3.0 or .net 3.5 (i.e. VS2008)?
-
September 1st, 2014, 04:19 AM
#3
Re: Web Api
Hi,
Yes, the codes was not supported. So I amend my codes to below:
Code:
public string UploadFile(string WebApiBaseAddress, string WebApiServiceUrl, WebApiParameters webParams)
{
string eFile = null;
RestClient client = new RestClient();
client.BaseUrl = "http://localhost:8055/api/efile";
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddObject(webParams);
IRestResponse response = client.Execute(request);
eFile = response.Content;
return eFile;
}
But the program is not working at all and prompt the following error:
{"Message":"An error has occurred.","ExceptionMessage":"The given key was not present in the dictionary.","ExceptionType":"System.Collections.Generic.KeyNotFoundException","StackTrace":" at System.Collections.Generic.Dictionary`2.get_Item(TKey key)\r\n at EFileServerWebAPI.Controllers.EFileController.PosteFile(WebApiParameters wParams)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
I am sure that my web api function is working fine. But there might be error on the above calling function. Actually what I want to do is just pass the WebParams to my Web Api. May I know is it something wrong with my codes?
-
September 1st, 2014, 02:51 PM
#4
Re: Web Api
Debug the cide.
Put a breakpoint on the first line of code inside the controller method (F9)
Start debugging the web app (F5)
Navigate through the app to the page where it hits the controller method
Then single step (F11) through each line in the method.
At that point you can see which line causes the exception.
You can hover over the dictionary variable and see if a key already exists.
Once you isolste the problem line you can figure out what to do.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|