RredCat
RredCat

Reputation: 5421

Web api project routing trouble

I have simple web API project (ASP.NET MVC). I need to deploy my project in the subfolder on the IIS. For example, I have the site http://TestSite/ and I need to deploy the project to http://TestSite/MyProject/. When I did it, web API routing stopped to work. Because my ajax call is routed to the main site - http://TestSite/api/something/get.

I have tried to update map routing in the next way:

routeTemplate: "MyProject/api/{controller}/{id}",

But it doesn't affect as I want. What I am doing wrong and where can I read about some practices of web API control routing in ASP.NET MVC?

Upvotes: 1

Views: 408

Answers (1)

RredCat
RredCat

Reputation: 5421

It was pretty simple. I didn't need to do anything with routing. I just needed to change url in JavaScript.

From:

$.getJSON('/api/Category', function (data) {

To:

$.getJSON('api/Category', function (data) {

Just remove '/' symbol before 'api'. Be aware.

BTW, nice article about debugging ASP.NET Web API - Debugging ASP.NET Web API with Route Debugger

EDIT: Forget that I wrote before (instead of the BTW section). This doesn't work. It works for http://TestSite/MyProject/ but doesn't work for http://TestSite/MyProject/MyController/Index. I met such kind of issue with my past ASP.NET MVC project. And I solved it by starting to use Url helper e.g: @Url.Action("MyAction","MyController"). So I needed something like for Web Api. And I found it. It is UrlHelper.HttpRouteUrl method. Now my code looks in the next way:

$.getJSON( "@Url.HttpRouteUrl("DefaultApi",new {controller = "Category"})", function (data) {

Where:

  • DefaultApi - name of my default rote.
  • Category - name of my api controller.

This solution doesn't look elegant but it works.

Upvotes: 2

Related Questions