.NET Web Api - 404 - File or directory not found - asp.net-mvc

In short words, everything works on local machine but doesn't when deployed to server "A".
I'm sure someting is missing in server "A", but I don't know what it is, and I don't have access there to make changes or proper testing.
So I'm here to aks for help and ideas of what the problem may be so that I can advise the person in charge of server "A", or whatever changes I need to make on my project to make it work there.
That being said, lets talk about my project...
I'm using Web Api, and a route is defined in the global.asax as such:
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
I'm calling the controllers using Jquery ajax and Kendo UI, as such:
(this is a fake example just to prove the point)
read: {
url: function () {
return "../api/Member/123";
}
}
Again, everything works great on local server, but when I deploy it, I get for every web api call:
404 - File or directory not found.
Nothing else.
Things to consider:
I'm compiling the project with .NET 4.0 framework.
The website on server "A" is set up to use .NET 4.0 classic (could this be a problem?)
I don't think MVC 4 is installed in server "A". Does Web Api requires MVC?
Server "A" is running IIS 7
I added the following to the web.config file, but still get the 404 error:
<modules runAllManagedModulesForAllRequests="true"/>
Let me know if you need any other information.
Thanks for your help!!

Download MVC4 to use ASP.NET WebAPI. http://www.microsoft.com/en-us/download/details.aspx?id=30683

Related

Problems (retroactively) adding Web API to asp.net MVC project

I'm taking my first steps with asp.net mvc in Visual Studio 2013 and I've run into a weird problem.
I started my Project as MVC and did NOT check the "Web API" box to include the core references. However now I want to use the Web API functionality. I created a new "somethingController" controller, selected API controller (empty) and Visual Studio did it's magic, creating a routing file, etc.
However when trying to access the URL /api/something/ I geta 404. I believe the routing doesn't work or there is some other problem I haven'T thought about yet...
It works just fine if I create a new Project and checkt the "Web API" box. But if you have a larger project and want to add Web API functionality there must be an easy way to reliable add it to the project, right?
Could anybody please explain
a) Where the problem lies (routing? Missing references? something else?)
and
b) How can one reliably add the Web API stuff to a project that wasn't started with the checkbox checked.
Thank you very much.
Ok, seems I got it working. When you add your first API controller to a project that does not have the Web API checked, a readme file pops up with some additional information:
I tried adding
WebApiConfig.Register(GlobalConfiguration.Configuration);
to the global.asax and it did not work. the readme show the following code
GlobalConfiguration.Configure(WebApiConfig.Register);
that DOES work, although it seems (to me) that both lines do exactly the same thing??
Have you added WebApi.Config file
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
add this in your global.asax
WebApiConfig.Register(GlobalConfiguration.Configuration);

Calling Web API from MVC Application when both in same Solution

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

hosting ASP.NET MVC 4 web api on IIS - failed to load resource

I'm trying to set up an API and test it, (but my knowledge of ASP.NET MVC and IIS are limited). I'm using the sample code provided at http://www.asp.net/web-api/overview/creating-web-apis (Sample: Contact Manager).
I uploaded all the provided files from the ContactManager folder (App_Data, Content, etc.) and then set up a new website in IIS with the IP 142.70.43.211 and the Home Directory pointing to the local directory containing the files.
The Default.htm file loads fine, but the Jquery ajax call returns an error "Failed to load resource: the server responded with a status of 404 (not found)".
At first, I thought it was the fact that the jquery get() call is requesting just "contacts" rather than the complete url, but Chrome's Console shows me it is trying to load "142.70.43.211/contacts" and failing...
EDIT: I forgot to upload the "packages" folder that was provided...after doing this, the console gives me the error: "GET 142.70.43.211/contacts 404 (Not Found)"...not much different...
Did I miss a step in setting something up?
The WebApiConfig.cs file shows the routes defined as follows:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Which means its expecting /142.70.43.211/api/contacts. Check if Views\Home\Index.html has reference to the paths "api/contacts" and not just "contacts"
I was getting 404 errors until I went to the IIS manager for the site -> Basic Settings, and changed the application pool to "ASP.NET v4.0".

Using Cassini with a virtual path that contains dots

I am working on an existing project in ASP.NET witch is based in a folder that contains dots
http://localhost/My.Awesome.WebClient/
This setup works fine using the integrated Visual Studio Develompent Server, but fails when I add MVC content and try to access it (Error 404 Resource not found).
http://msdn.microsoft.com/en-us/library/ee941656.aspx seems to explain the issue:
If you create a file system Web site in Visual Studio 2010 and the Web site is in a folder that contains a dot (.) in the folder name, URL routing will not work reliably. An HTTP 404 error is returned from some virtual paths. This occurs because Visual Studio 2010 launches the Visual Studio Development Server (Cassini) using an incorrect path for the root virtual directory.
However, the project is a Web Project, not a Web Site, and it only failed when I started using MVC.
The problem can be easily reproduced:
File - New - Project - ASP.NET MVC 3 Web Application
Edit project settings, Web: Use Visual Studio Development Server
Set the virtual path to something that contains a dot
Try to run the site
Is there a way to get this to work, besides using IIS instead of Cassini?
Edit:
I did find a workaround just now. It does not really work in a deployment scenario, but it may help in finding a solution:
in my Global.asax.cs file:
routes.MapRoute(
"Default", // Route name
// Notice that I added the virtual path here
"My.Awesome.WebClient/{controller}/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// This is also new
routes.MapRoute(
"Root", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Using this workaround, my MVC-Sites work, but only with the following url:
http://localhost/My.Awesome.WebClient/My.Awesome.WebClient/
If you don't get an answer to this question (personally, I'm not aware of any way around this), I would recommend using "IIS Express", which is available as a free download. It can be installed on development machines and you can control a lot of server settings with your web.config file, which is fairly unobtrusive.
You can download it here:
http://www.microsoft.com/download/en/details.aspx?id=1038
I use IIS Express exclusively now; Cassini has always lacked features and IIS Express avoids me having to configure IIS for each web application.

I am not able to run Asp.Net MVC 2 Application

I am trying an sample application in Asp.Net MVC 2 Application
given in http://www.codeproject.com/KB/aspnet/aspnet_mvc_tutorial.aspx
But i am getting the error as below
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Views/PersonalInformation/Index.aspx
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
Please let mw know wat is the problem
The problem is the requested url. In an ASP.NET MVC application, the url corresponds to a controller action instead of the aspx page. See this link for an explanation: http://www.asp.net/LEARN/mvc/tutorial-02-cs.aspx.
The url should be something like http://localhost:xxxx/PersonalInformation (where xxxx is your port number)
To avoid getting this error everytime you start up your website through Visual Studio. Go to the properties of your web project and under the Web tab there is a property called Start URL. Change this to http://localhost:xxxx/ or something that is a valid MVC route.
Are you deploying to Cassini or IIS 6. Take a look at these articles
http://guyellisrocks.com/coding/deployed-asp-net-mvc-app-gives-404-on-about-page/
http://www.techdreams.org/microsoft/aspnet/how-to-fix-404-errors-of-aspnet-mvc-website-deployed-on-iis-6-windows-server-2003/2572-20090515
MVC runs fine in Cassini but requires some config modifications to run in IIS. I am still evaluating it in Cassini so i havent crossed that bridge yet but this info should help.

Resources