【问题标题】:Web Api 2 404 errorWeb API 2 404 错误
【发布时间】:2016-02-02 20:18:59
【问题描述】:

尝试使用以下 url 调用以下方法时遇到 404 错误

    [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

此 url 来自对字符串“Tax H: AG43SS GMIB; no fpws; d&r”调用 HttpUtility.UrlEncode()。如果有另一种方法来完成传递这个字符串,我也愿意接受。

如果我将其称为“更简单”的任务名称,则该方法可以正常工作

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

我安装了 RouteDebugger nuget 包,但它不是很有帮助。为了完整起见,这里是我的 WebApiConfig.cs 文件

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

和RouteConfig.cs文件

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

【问题讨论】:

    标签: c# asp.net-web-api routing


    【解决方案1】:

    您的 api 调用失败的原因是 URL 中的字符 % 和 +。构造或设计您的 api 调用 URL 以消除这些字符,就像您成功的 api URL 一样

    【讨论】:

      【解决方案2】:

      您收到的错误是:

      HTTP 错误 404.11 - 未找到
      请求过滤模块被配置为拒绝包含双转义序列的请求。

      要解决此问题,您可以在 web.config 文件中应用一些设置:

      1- 在&lt;system.webServer&gt;&lt;security&gt; 你应该有:

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

      2- 在&lt;system.web&gt; 下你应该有:

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

      第二个设置允许您在路径中包含:

      【讨论】:

      • 它适用于该任务名称,但现在无法用于任务名称“AG33/34”,我应该问一个新问题还是编辑这个问题?
      • 它会返回一个不同的错误,如果您使用合适的 url 编码,则不会收到该错误。您应该使用 %2F 而不是 /。像 %3a 和 %3b 和 ...
      • 我正在使用 HttpUtility.UrlEncode,它为“AG33/34”返回“AG33%2f34”,现在我收到此异常“没有 MediaTypeFormatter 可用于读取类型为“KeyValuePair”的对象'2' 来自媒体类型为 'text/html' 的内容。”
      • 在路由参数中使用 /%2f 似乎会产生一些问题,但如果在查询字符串中使用它们就没有问题。无论如何,感谢您的反馈并接受答案:)
      • 我用于该问题的解决方案是在路由定义中使用{*taskName} 而不是{taskName}
      猜你喜欢
      • 2017-12-04
      • 2015-08-08
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多