Simon Kiely
Simon Kiely

Reputation: 6050

HTML link is not to a valid folder...but it still works?

I'm having some trouble understanding a bit of HTML and was hoping that SO could help me in the process of figuring out what is going on, so that I might be able to do this myself in the future.

The markup is simple :

<p>
  GET <a href="~/api/function">/api/function</a>: returns list of info from database.
</p>

Now, this works fully and I am trying to understand what is going on. My understanding is that this would go to root directory, find a folder called api, find a function called function and run it.

The problem is that there is no folder called api - so what could be going on here ? I can find the C# function that is actually being called to retrieve the items from the DB, but I cannot figure out how the code calling this might be structured. I have a class which extends DbContext to retrieve information, but I cannot see how this is being called and it is not on the call stack when I insert a breakpoint.

Can anyone give me some information on how I might shed some light on this ?

(Apologies for the very general question, I will give more specifics as I begin to understand what is actually going on!)

Upvotes: 0

Views: 74

Answers (2)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

There is actually no need for an api folder. It can be a simple route configured that maps a certain URI scheme to some files. It doesn't even have to be files, it can be methods on a class.

For example, in ASP.NET Web API, you have ApiController classes with methods. In your case the method would be called Function or GetFunction or similar.
The route config would contain something like that:

routes.MapHttpRoute("SomeRoute",
                    "api/{action}",                           
                    new { controller = "YourController", action = "Index" });

See the introduction to routes in ASP.NET Web API for more info.

I suggest that you also read the entire series about the ASP.NET WebAPI

Upvotes: 2

ATOzTOA
ATOzTOA

Reputation: 36000

"~/api/function" need not be an actual folder in your filesystem. It can be a virtual path defined in your webserver configuration, like web.xml in tomcat.

Upvotes: 0

Related Questions