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
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.
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
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
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.
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.
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
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
Can you post your api controller?
Giannis