Http route without parameters and a point inside the route

风流意气都作罢 提交于 2020-05-17 07:30:06

问题


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();


回答1:


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.



来源:https://stackoverflow.com/questions/52111209/http-route-without-parameters-and-a-point-inside-the-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!