简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM