【问题标题】:HttpControllerSelector not finding my controller classHttpControllerSelector 找不到我的控制器类
【发布时间】:2017-12-13 15:28:28
【问题描述】:

我需要开发一个 Web 应用程序,它应该响应诸如“api/ping.v1”之类的 URL(URL 是在外部指定的,没有更改它们的选项)我遇到了问题,因为 HttpControllerSelector 没有找到我的控制器类。我总是得到“未找到”的结果。 不知道,怎么配置路由,才能正确找到我的控制器类。这是一些代码,我到目前为止所做的:

控制器类:

namespace WebApplication1.Controllers
{
    public class PingController : ApiController
    {
        [HttpPost, Route("api/ping.v1")]
        public PingResponse HandleRequest(PingRequest request)
        {
            // TODO: Handle request and create Response object.
            return new PingResponse();
        }
    }
}

WebApiConfig.cs:

namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web-API-Konfiguration und -Dienste

            // Web-API-Routen
            config.MapHttpAttributeRoutes();

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

PingRequest.cs:

namespace WebApplication1.Models
{
    public class PingRequest
    {
        [StringLength(20)]
        public string UserDms { get; set; }

        [Required, StringLength(7)]
        public string Ipn { get; set; }

        [Required, StringLength(8)]
        public string DealerId { get; set; }

        [Required, StringLength(20)]
        public string WorkshopPartshopId { get; set; }
    }
}

类 PingResponse 完全是空的,所以我不在这里发布这个类。

我用于测试 Web 应用程序的 HTML 页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Ping Test</title>
</head>
<body>

<div>
    <h2>Ping</h2>
    <input type="button" value="Ping" onclick="ping();" />
    <p id="pingResult"/>
</div>

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
    function ping() {
        $.post('api/ping.v1',  {userDms: "wb", ipn: "Ipn", dealerId: "dealer", workshopPartshopId: "wshop"})
            .done(function (data) {
                $('#pingResult').text(JSON.stringify(data.response)).css("color", "black");
            })
            .fail(function (jqXHR, textStatus, err) {
                $('#pingResult').text('Error: ' + err);
            });
    }
</script>
</body>
</html>

当我从 Visual Studio 启动我的 Web 应用程序时,我可以在浏览器中使用“Ping”按钮查看我的测试站点。单击按钮 ping 后,我将收到消息“错误:未找到”。

我希望有人能指出正确的方向,我的路由配置出错了。

最好的问候 迈克尔

【问题讨论】:

  • 问题不在于您的网址。它的'。在您的 API 中。它将被视为扩展并像静态文件请求一样使用。 stackoverflow.com/questions/20998816/…
  • 为什么要重复我的回答? :)

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


【解决方案1】:

您的问题是由 URL 中的那个点引起的。 IIS 认为 Url 指向静态文件,甚至不执行路由逻辑。如果您无法避免此类 Url,您可以通过将 runAllManagedModulesForAllRequests 属性添加到 web.configmodules 部分来更改此行为:

<modules runAllManagedModulesForAllRequests="true">
<!-- ... -->
</modules>

查看question了解更多详情。

【讨论】:

  • 非常感谢您快速而有帮助的回答。 web.config 中的这个条目解决了我的问题。
猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2018-12-15
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多