reggaeguitar
reggaeguitar

Reputation: 1804

Web Api 2 404 error

I'm getting a 404 error when trying to call the following method with the following url

    [HttpGet]
    [Route("api/Trinity/GetDirectoryAndTask/{modelTemplateId}/{taskName}")]
    public KeyValuePair<string, string> GetDirectoriesAndTasks(int modelTemplateId, string taskName)

http://localhost:46789/api/Trinity/GetDirectoryAndTask/9/Tax+H%3a+AG43SS+GMIB%3b+no+fpws%3b+d%26r

This url comes from calling HttpUtility.UrlEncode() on the string "Tax H: AG43SS GMIB; no fpws; d&r". If there's another way to accomplish passing this string I'm open to that as well.

The method works fine if I call it ith a "simpler" task name like so

http://localhost:46789/api/Trinity/GetDirectoryAndTask/9/Roth

I installed the RouteDebugger nuget package but it wasn't very helpful. For completeness here is my WebApiConfig.cs file

    using System.Web.Http;

    namespace DbService
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API routes
                MapRoutes(config);
            }

            private static void MapRoutes(HttpConfiguration config)
            {
                config.MapHttpAttributeRoutes();

                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //);
            }
        }
    }

and RouteConfig.cs file

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    namespace DbService
    {
        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 }
                );
            }
        }
    }

Upvotes: 3

Views: 1051

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

The error you are receiving is:

HTTP Error 404.11 - Not Found
The request filtering module is configured to deny a request that contains a double escape sequence.

To solve the problem you can apply some settings in your web.config file:

1- Under <system.webServer> under <security> you should have :

<requestFiltering allowDoubleEscaping="true"></requestFiltering>

2- Under <system.web> you should have:

<httpRuntime targetFramework="4.5" requestPathInvalidCharacters="" />

The second setting allows you to have : in your path.

Upvotes: 3

Julius Depulla
Julius Depulla

Reputation: 1633

The reason your api call is failing is because of the characters % and +, in the URL. Structure or design your api call URL to eliminate these characters as in the case of your successful api URL

Upvotes: 1

Related Questions