Calling Web API from MVC Application when both in same Solution - asp.net-mvc

So I am totally new to the Web API and I thought to myself I would start a test project to learn it and AngularJS and how they can work together nicely ... etc.
I have created a multi-tier architecture within my solution, and inside my TPlan.API project, I have the following route configured in the Global.asax
GlobalConfiguration.Configuration.Routes.Add("default",
new HttpRoute("api/{controller}"));
TPlan.WEB is my MVC application, and it is set up as "Start Up Project". When I run it, I want to be able to go to:
mysite:port/api/test
And get the value from the API from my test controller in there.
However it is not working for me, I get the following:
No HTTP resource was found that matches the request URI 'mysite:port/api/test'.

Er, in visual studio, right click on the solution and go to properties.
Go to startup and select the "multiple projects" option and tick the website and the webservice to both start up.
That will start both of them. But as has been pointed out, they're running on separate ports, they're completely separate applications...
I don't think you can register a route that belongs outside of the website.
We work like this
View => POST / GET => MVC Controller => HTTP Request => API Controller
So our mvc views post or get to our mvc controllers, and then we fire off a separate http request to the web api. this is a server to server call, its like calling any external web service. we wait for the response from the api and then return whatever is required to the view...

What you are attempting isn't logically possible without installing your WebAPI project into IIS ahead of time, which I'm sure is not what you want. Both projects cannot be run at the same time, as only one project will be able to launch a debug session of IIS Express. Even if both projects were able to run at the same time, they would be on different logical ports, and routing from one project would have to be manually sent to the listening port of the other instance. Basically, your Global.asax on your API project will never run, as that project will be built as a class library.

Make your TPlan.API project a simple Assembly, reference it from TPlan.Web and pass the GlobalConfiguration.Configuration property over to a Register method that is in your API assembly. When the MVC project starts, both the MVC routes and the Web Api routes will be active on the same web host.
Here is an example of an API that has both a console host and a web host in the same solution.

Please check the following site. I believe the issue lies in the configuration of the route
http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api

Related

How to debug MVC web app with rest api controller backend? [duplicate]

In VS 2012, I am attempting to create an MVC 4 web application with jQuery calls to a Web API project. (Other devs will be consuming the API with our current, native app, and probably adding to the API in the future.) So I have one project that is the Web API, and another project that is the MVC 4 website. I can only set one of them to run, and they use localhost:xxxxx.
How do I debug changes to both? For example, let's say I add a new API path /api/customer/get and then a new jQuery ajax call to that path and do something with the resulting JSON. I've changed code in both projects and want to follow it end-to-end; how do I launch both? How do I debug both?
Just to be clear, the MVC app isn't making server-side calls to the API, I'm using MVC mostly to be able to easily use bundling, minification, and (hopefully) pre-compiled Handlebars templates in .NET; the API calls are coming from jQuery. As I am still relatively new to these technologies, alternate suggestions are welcome.
Thank you in advance.
I had the same problem and have found a solution from here:
forums.asp.net
The fix is to do the following:
In your solution file, click properties go to the Startup project node (if it is not already selected)
Next select Multiple startup projects. Select your website and your webservice and in the Action column make sure both of them have "Start" selected.
Now when you debug your website and put a break point in your webservice, it should hit the break point.
Coming late to the party but in case anyone else is looking for a solution, this is what was best for me: Set the Api project up to be the starting project (I needed to limit to one startup so that I could flip between browsers more easily). After firing up the service project, right click on the web/ui project and select debug, start new instance. You'll have both running and you'll seamlessly step from web to api.
I had a similar problem with my web api project. My solution consisted of an angular front end with 2 web api projects on the backend. One web api project handled "authorization" and the other handled "resources". I used the following tutorial by Taiseer Joudeh as a starting point:
http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/
Breakpoints worked on the "authorization server"... but not on the "resource server". I compared the packages from the two projects to see what was different. Once I added "Microsoft.AspNet.WebApi.Cors" to the "resource server" project, the breakpoints starting working.

Web application in ASP.NET 5

I am building a Web Application in ASP .NET 5 using Visual Studio 2015. I have created a solution with Data Access, Business, Services and User Interface layers.
I have referenced the Services in User Interface layer.Since in MVC 6 both Web API and MVC fall under the same project template, it is necessary to have two different layers for services and UI or same project with different controllers is enough.?
And also in the UI project I have uncommented the following lines in Startup.cs
services.AddWebApiConventions();
routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
While running the project, the home page comes up fine but when I click on links in the home page the url changes like this
http://localhost:45075/api/Home
And it gives error page saying that
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied.
Sample.Services.Controllers.HomeController.Index
Sample.Services.Controllers.HomeController.About
Sample.Services.Controllers.HomeController.Contact
Sample.Services.Controllers.HomeController.Error
My understanding is that since I enabled Web Api conventions, it is going to the Home Controller of services which has route attribute
[Route("api/[controller]")]
If so how resolve this conflict by specifying namespace or some constraints while rendering views in the routes.
I am a beginner. Correct me if I am wrong.
On MVC 6 you can have RESTful APIs as long as Views in the same project and even on a same controller.
One ways to set up routing for you application is to create a map on Startup.cs like this, with IApplicationBuilder:
Then, you can specify the routes for each action:
By setting a [HttpGet] (or any other HTTP verb) on the Action without parameters, it will create a route by convention, following you action's name. That way, you can solve many conflicts. It's also possible to put multiple actions of a same HTTP verb on one Controller.
And as stated above, please ask only one question per post.

Web API in an MVC project not working after publishing

I have an mvc project which has a web api controller. The web api has a Get method which returns a list of objects. This call is made from the client using JQuery ajax method and the data is exchanged as JSON object. Everything works fine when I run the application from within Visual Studio. The pages load correctly and on clicking the 'Search' button, the ajax call is made and the records are loaded successfully on the page. The routes are properly mapped to the areas and there is absolutely no problem.
The problem arises when I publish the application in IIS and then try to 'Search' the records. On clicking the 'Search' button, I get 500 Internal Server Error which I believe suggests that the web api service is not available to the application and is not running.
Do I need to deploy/publish the web api separately? If thats the case then how can I do that deploy it separately given that it is part of the same MVC project (there is no separate Web API project).

WCF showing the "You have created a service." page instead of WSDL

I have a WCF service running inside an ASP.NET MVC 4 application. The site is running on IIS8 on Server 2012. Until recently, the WSDL pages loaded without any problems. This week, the WSDL no longer loads. The link from the "Welcome" page is correct, but when I click it or attempt to generate a client, the same Welcome page is returned.
I have tried this using a ServiceRoute and also using an .svc file.
The ServiceRoute looks like this:
/DataService
Its WSDL link is:
/DataService?wsdl
The .svc file looks like:
/directory/otherdir/DataService.svc
The WSDL link is
/directory/otherdir/DataService.svc?wsdl
I never needed any configuration in the web.comfic section before, using the SerivceRoute.
How can I get WCF to show the WSDL correctly?
I ran into this before and there can be multiple reasons, the service is not functioning due to errors in the operations or you may be using WebServiceHostFactory instead of ServiceHostFactory
RouteTable.Routes.Add(new ServiceRoute("DataService", new ServiceHostFactory(), typeof(MyServiceType)));

Best way to deploy an ASP.NET MVC app on IIS 7 / Server 2008?

So far, the only successful strategy I've been able to get by with is the following:
Configure YOURAPP.Web to "Use Local IIS Web Server" and set "Project Url" = http://localhost/yourapp.web
Click "Create Virtual Directory"
In IIS Manager ensure that "Classic .NET App Pool" is selected
Add wildcard mapping * named "ASP.NET-MVC" that points to IsapiModule
BUT I don't really like the idea of having the full ASP.NET pipeline invoked when requesting any resource (javascript file, stylesheet, image, etc...) which is exactly what happens with the wildcard mapping.
So is there a way around this?
Is there a better method to running mvc apps on IIS 7?
I'm definitely open to suggestions as I'm not all that satisfied with what I got so far.
Thanks - wg
Make sure IIS7's "Managed pipeline mode" is set to "Integrated". That has been the single most important thing to do (for me) to get my MVC app running smoothly on IIS7.
Here's a good blog post too.
When developing the app, I have had no issues using the "Visual Studio Development Server" for running the app.
Assuming that all of your CSS/JS/image files reside in the Content directory, you could add an ignore route.
routes.IgnoreRoute("Content/{*wildcard}");
This will prevent any requests to the content directory being handled by MVC.
IIS7 is designed to take all requests and push off the static file stuff efficently. I wouldn't worry about it in the same way one worries about wildcard mappings on IIS6. There is no "load ASP.NET pipeline" penalty because IIS7 is a mean honking ASP.NET pipeline all the time.

Resources