0

I've got an API app up and running on my localhost. It works great at an endpoint such as:

http://localhost:26307/api/ModelName/12345

However, after deploying to the Test server, the same URL needs to be:

http://v10test.mydomain.com/api/api/ModelName/12345

I'm trying to get rid of the double /api/.

I adjusted my routes FROM THIS (for example):

config.Routes.MapHttpRoute(
    name: "route2",
    routeTemplate: "api/{controller}/{organizationSys}"
);

TO THIS.....

config.Routes.MapHttpRoute(
    name: "route2",
    routeTemplate: "{controller}/{organizationSys}"
);

NOTE: I REMOVED "api/" FROM THE ROUTES.

But that seemed to have no effect. In IIS, the API's are set up as an application within the domain:

enter image description here

I'm probably overlooking something obvious, but I'm just not sure what. How do I get rid of that double /api/?

9
  • 2
    You're deploying to the virtual 'api' app and then the route takes effect beyond that. Commented Mar 15, 2018 at 15:29
  • Sure, so then without having api/ in the route, where is the need to have api/ in the url coming from? Commented Mar 15, 2018 at 15:30
  • I think your second configuration should be right. After you deploy it you might have to recycle/restart the api app. Maybe the config is still in memory. Commented Mar 15, 2018 at 15:34
  • 1
    Check that your controller doesn't override the path with its own Route attribute Commented Mar 15, 2018 at 15:45
  • 1
    AH - HA!! nailed it Hoots. If you put that as an answer, I'll mark it as such. (I knew it was something obvious.) Commented Mar 15, 2018 at 15:48

2 Answers 2

1

There are several ways to specify routes to a controllers actions, and the order of precedence matters.

Firstly, it can be done from a general configuration, e.g. as you've done it...

config.Routes.MapHttpRoute(
    name: "route2",
    routeTemplate: "{controller}/{organizationSys}"
);

However this can be overridden by specifying a Route attribute on the controller or by specifying the route in the verb attribute. For example in the code below...

[Route("api/[controller]/[action]")]
public class TestController : Controller
{
    [HttpGet]
    [Route("/api")]  // url... /api
    [Route("/api/test")] // url... /api/test
    [Route("testalso")] // url... /api/test/get/testalso
    public string Get()
    {
        return "Alive";
    }

    [HttpGet("/api/echo/{id}")] // url... /api/echo/{id}
    public string Echo(string id)
    {
        return $"Get Echo: {id}";
    }

    [HttpPost("{id}")]  // url... /api/test/postit/{id}
    public string PostIt(string id)
    {
        return $"Thanks for {id}";
    }
}

The declaration on the controller specifies a default for all methods within the controller and any methods that specify attributes can either override the controller by starting the route with a '/' or append to the controller's route. See the comments next to each attribute above for examples.

Beyond that the routes will be relative to the base application hosted within iis in your case which starts at...

http://v10test.mydomain.com/api/

Hope that's enough information for you.

Sign up to request clarification or add additional context in comments.

Comments

1

It is because of the way it is deployed in IIS. For IIS your application is at address

http://v10test.mydomain.com/api/

And after that comes all the controllers and etc. If you deploy it to (for example)

http://v10test.mydomain.com/myapp/

the calls will work like

http://v10test.mydomain.com/myapp/api/ModelName/12345

EDIT

Also - check the routes of your controllers or methods inside the controllers. This is the other place where the route may be modified/extended.

9 Comments

Ok, that makes sense. But I thought that removing the api/ from the routes would also remove the need to have api/ in the url at all.
Removing it from the routes will end up with only one /api/ because of the virtual directory, that it is deployed to
Maybe I wasn't clear in my OP, but I DID remove it from the route, and it STILL requires the double /api/api/
And this is the only route that you currently have?
No, it's just an example. But I removed "api/" from all of them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.