Web api routing issue in iis virtual directory

Topics: ASP.NET Web API
Mar 24, 2014 at 6:34 PM
Edited Mar 24, 2014 at 6:35 PM
I experience a 404 error when ever i publish a webapi project to a virtual directory.

e.g
website is codeplex.com
virtual directory is codeplex.com/vd


var routeb = routes.MapHttpRoute(
                   name: "DefaultApi4",
                   routeTemplate: "vd/api/{controller}/{action}/{id}",
                   defaults: new { id = System.Web.Http.RouteParameter.Optional, action = System.Web.Http.RouteParameter.Optional }
               );
Coordinator
Mar 24, 2014 at 6:48 PM
Your route template should be relative to the vdir. If you remove the vd prefix from your route template does that resolve your issue?

Daniel Roth
Mar 24, 2014 at 6:55 PM
Edited Mar 24, 2014 at 6:59 PM
that was the first thing I tried.no it doesn't
Coordinator
Mar 24, 2014 at 7:15 PM
OK. Could you please show us the exact route you are using, the controller and action you are trying to hit and the request URI you used?

Daniel Roth
Mar 24, 2014 at 7:27 PM
var routeb = routes.MapHttpRoute(
                   name: "DefaultApi4",
                   routeTemplate: "api/{controller}/{action}/{id}",
                   defaults: new { id = System.Web.Http.RouteParameter.Optional, action = System.Web.Http.RouteParameter.Optional }
               );
http://domain.com/vd/api/pages

this returns a 404 error.
i will suggest ur create a simple webapi project with the default values controller. publish it to virtual directory within a website. it appears impossible to reach. the routing some how breaks. I tried putting the route in the global.asax of the site in the virtual directory and in the root website. it doesnt work.
Coordinator
Mar 24, 2014 at 7:38 PM
So, if this is your URI template: "api/{controller}/{action}/{id}".

And this is the request URI: "http://domain.com/vd/api/pages".

Then the route will not hit because you have a required {action} route variable that is not provided in the request URI.

I assume you want to call an ApiController called PagesController? If yes, then try remove the {action} variable from your route. Also, you probably want this route comes before any MVC routes that you might have.

Hope this helps.

Daniel Roth
Coordinator
Mar 24, 2014 at 7:53 PM
Oh wait, you have {action} as optional too :-).

Do you have a PagesController?

What other routes do you have in your route table?

Daniel Roth
Mar 24, 2014 at 7:55 PM
thank for the help but u can c from my route that action parameter is optional. It works perfectly on my local machine but when uploaded to a virtual directory the system is broken. Just try publishing a sample webapi project to a virtual directory in iis.
thanks for the effort
Mar 24, 2014 at 7:57 PM
Edited Mar 24, 2014 at 8:44 PM
maybe I should do a short youtube video on this.
Coordinator
Mar 24, 2014 at 8:09 PM
I tried a default Web API template on local IIS with a vdir with Visual Studio 2013 Update 1 on IIS 8.5 and it worked fine.

Daniel Roth
Mar 24, 2014 at 9:32 PM
ok dan here it is. http://youtu.be/b3edh8OhK38
Developer
Mar 24, 2014 at 10:00 PM

Thanks for the video :-)...You would need to convert the ‘vd’ here to an application (ex: right click on the virtual directory and select ‘Convert to Application’) and now your requests should succeed…

Marked as answer by ojbilly on 3/24/2014 at 3:27 PM
Mar 24, 2014 at 10:11 PM
ok, thanks but how do I do it in a remote host, is there a way to indicate that in the webconfig. Thank you very much.