如何支持这两种 URL

How to support both kinds of URLs

我希望能够通过以下两种方式调用 GET:

/api/Test/Test/1

/api/Test/Test?id=1

public class TestController : ApiController
{
    [HttpGet]
    public string Test(int id)
    {
        return "Test";
    }
}

如何配置路由?

我有默认值:

            config.MapHttpAttributeRoutes();

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

这使得“/api/Test/Test?id=1”有效但“/api/Test/Test/1”

无效

我尝试在操作上添加属性路由,这使得“/api/Test/Test/1”可以工作,但现在“/api/Test/Test?id=1”不起作用:

        [HttpGet]
        [Route("api/test/test/{id}")]
        public string Test(string id)
        {
            return "a";
        }

使用 ASP.NET Web API 模板,我这样配置 WebApi 路由:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}"
);

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

您需要小心处理包含在控制器中的操作。例如,我有这样的 ValuesController

public class ValuesController : ApiController
{
    [HttpGet]
    public string Test(string id = "")
    {
        return "test " + id;
    }
}

如上所示配置路由后,api 将响应以下请求:

/api/values/2
/api/values/test/2
/api/values/test?id=2

但是,如果您在同一个控制器中添加第二个 HttpGet 操作,事情就会变得复杂。服务器在尝试响应映射到第一条路由 (DefaultApi) 的请求时,将无法确定要发送哪个 get 操作。 DefaultApi 路由依赖于一个约定,即控制器将只有一个操作来为每种 HTTP 请求提供服务。如果您添加名为 Try:

的第二个 HttpGet 操作
[HttpGet]
public string Try(int id)
{
    return "try " + id;
}

服务器将响应如下:

/api/values/test/2    -> WORKS
/api/values/test?id=2 -> WORKS
/api/values/try/2     -> WORKS
/api/values/try?id=2  -> WORKS
/api/values/2         -> DOES NOT WORK, Multiple GET methods. server can't decide which to use

如果您打算在同一控制器中对每种类型的 HTTP 请求使用超过 1 种方法,我建议删除 DefaultApi 路由并仅保留 ActionApi。另一个例子:

// Removed "DefaultApi"
config.Routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
public class ValuesController : ApiController
{
    [HttpGet]
    public string Test(int id)
    {
        return "test " + id;
    }

    [HttpGet]
    public string Try(int id)
    {
        return "try " + id;
    }

    [HttpGet]
    public string Play(string str)
    {
        return "play " + str;
    }
}

现在,服务器将成功处理以下请求:

/api/values/test/3       -> WORKS
/api/values/test?id=3    -> WORKS
/api/values/try/3        -> WORKS
/api/values/try?id=3     -> WORKS
/api/values/play?str=3   -> WORKS

但是,这些都行不通:

/api/values/play/3       -> DOES NOT WORK
/api/values/play?id=3    -> DOES NOT WORK

没有为方法 "Play" 调用参数 "id",服务器不知道您想要什么。回应:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'https://localhost:44327/api/values/play/3'.
    </Message>
    <MessageDetail>
        No action was found on the controller 'Values' that matches the request.
    </MessageDetail>
</Error>
/api/values/3

服务器期望控制器名称后面的内容(在本例中为 values)是控制器操作。 3 显然不是,所以服务器不知道如何处理请求。

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'https://localhost:44327/api/values/3'.
    </Message>
    <MessageDetail>
        No action was found on the controller 'Values' that matches the name '3'.
    </MessageDetail>
</Error>

我希望这个 post 能为您提供足够的信息来配置您的 API,但是您想要。