Avatar of lapucca
lapucca

asked on 

In asp.net MVC web app calling an API is not working for me

Hi, I'm using VS2013, asp.net MVC and C#
I created an ASP.net MVC project (not api project).  I added entity framwork and use EF6 to create a model form an existing database.  I then added a API coontroller to the Controller folder.  I used the web API 2 with Entity framework template(creates Rest api).  Then I added the code, attached, to the Index.cshtml file to call the api using the ajax.  

I run the project and everyone shows fine in the default.cshtml page except the list of content I expect from the api call db set.  All these are done within the same project.  is that the problem?  

I'm running this from VS2013 on my dev pc.  Is there a tool or way to test if my api is working?  how can I setup the test in SOAPUI, I downloaded a copy but not sure how to test Rest that runs on my local PC and in VS2013
Thank you.
Index.html
categories.html
ASP.NETWeb ServicesC#

Avatar of undefined
Last Comment
lapucca
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Hi,

Can you post your api controller?

Giannis
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Get a copy of Fiddler (http://www.telerik.com/download/fiddler) and the Chrome Postman app (https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop). Fiddler will let you see if the request you're sending is correct and Postman will let you craft the request manually and then send it to your API. If you're writing APIs these two tools are absolutely required in your toolbox.

Also, you make no mention of what the return code is from your API call. 200? 404? 401?
Avatar of lapucca
lapucca

ASKER

Loannis,
the attached categories file is the api controller file.
Craig,
I will installed those 2 tools, thank you.  I will update here about what I found using these tools, well, let's hope I can learned how to use them pretty quickly.
Avatar of lapucca
lapucca

ASKER

What should I enter for url when testing my web services that is running on my local dev pc, inside of my VS2013?  Thank you.
If you run your project, your browser will start And you will be able to see your local url. It will be something like http://localhost:12345

Just add /api/apicategories and you will get the results from your iqueryable method.
You may also check the url if you see the properties of your project.
Avatar of lapucca
lapucca

ASKER

Fiddler and Postman looks powerful but will take some time to learn.  
I kind of know SoapUI, so I create a new REST test in there. Then, I right clicked on my project, property, click Web on the left and it shows the project url to be http://localhost:19363/
So, do I put "http://localhost:19363/api/ApiCategories " as the endpoint(correct term?) when asked to enter the URI in Soap, is this correct for URI?

i run the Get in SOAPUI and got the attached error.  i also included the screen shot of the SOAPUI.  Would appreciate it if you can have a look at my categories file and see if I'm doing anything wrong or if I'm entering the wrong URI.
soapui-error.html
soapui-screen.jpg
Avatar of lapucca
lapucca

ASKER

Also, in the project=>property=>Application, the target .net Framework is 4.5.  But in my VS2013,
About visual studio, it shows .net 4.6.  Does that cause a problem?
Thank you.
VS2013.txt
First of all you do not need any tool to verify a web api get method. Just browse to that url and you will see the results.

Can you please post the contents of webapiconfig.cs in your App_start folder.
Avatar of lapucca
lapucca

ASKER

Loannis,
I ran the application in IE and invoked the Developer tool (F12), Network, then start Capturing.  At first it shows that it's calling my API with Home folder because I added the code in the Index.cshtml under the Home folder that calls it.  I then created a html file at the root of the project then call the api.  In IE it shows it's calling the right path now but still shows a 404 erro.  i attached both screen shots here.

1.  How do I ref api's uri when I'm calling api from a folder and not from the root?
2.  I suspect my problem is my project is not built with web API as part of the project template because under the App_Start folder there is not webapiconfig.cs file.  How to fix this?  
3.  Or should I just put my API code in a new project?   I think maybe this is better and cleaner.  I can keep my web application that I currently have and then put my web api in a separate project but both are under the same solution.

Thank you.
You may install web api through nuget.

Right click on your project and select Manage NuGet Packages. Then search for webapi.

Alternatively you may start NuGet Package console and run:

Install-Package Microsoft.AspNet.WebApi

After this you should now have web api installed.

Then you will be able to use that url directly on your browser.
Avatar of lapucca
lapucca

ASKER

I used Nuget, added Web Api 2.2 but no webapiconfig file in the App_Start folder.  I then added a new API controller, still no config file.  I restarted VS and still nothing.  

I went ahead and run the application and in the developer tools in IE it still shows 404 error.  I'm attaching screen capture here.

Thank you.
IE-1.jpg
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of lapucca
lapucca

ASKER

Loannis, that link was perfect.  I finally got it to hit my API code.  However, I'm getting all sorts of run time errors and http 500.  I will create a new web api project to start fresh.  Thank you.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo