Thomas
Thomas

Reputation: 2984

Http route without parameters and a point inside the route

I'm currently creating a webservice using ApiController classes. Due to some restrictions I have to put a Point in the route itself (terminal.journalentry). And use this as the base URL. Thus:

http://localhost:59684/terminal.journalentry

as example for a call. Now though I've run into Problems.

When I use the following code with this call: http://localhost:59684/terminal.journalentry it works without a hitch.

public class JournalController : ApiController
{
        [HttpGet]
        [Route("terminal.journalentry/{id}")]
        public void WriteJournalEntry(int id)
        {

        }
}

Though I Need to use the method without any Parameters involved. But when I try the following:

public class JournalController : ApiController
{
        [HttpGet]
        [Route("terminal.journalentry")]
        public void WriteJournalEntry()
        {

        }
}

With the call being: http://localhost:59684/terminal.journalentry I get:

HTTP Error 404.0 - Not Found

Now my question is: What is wrong there and what Needs to be done in order to use the above URL without running into Errors?

Edit as it was asked what the Content of Routes.config is:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

WebApiConfig.cs has only the following Content inside Register: config.MapHttpAttributeRoutes();

Upvotes: 0

Views: 63

Answers (1)

Craig H
Craig H

Reputation: 2071

I think you need to configure a handler to ignore the ".journalentry".
Usually anything with a fullstop is considered a file, and I imagine this is happening here.

In the web.config for your API, try and find the following section:
<system.webServer>
<handlers>

And add a handler definition:

<add name="JournalEntryExtensionHandler" path="*.journalentry*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

You might need to play around a bit with the path wildcard settings.
When I've done this previously the URL ended with a filename so the path was "*.jpg" for example.

Upvotes: 1

Related Questions