How to support both kinds of URLs

核能气质少年 提交于 2020-01-24 12:15:08

问题


I'd like to be able to call GET by both:

/api/Test/Test/1

/api/Test/Test?id=1

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

How to configure the route?

I have the default:

            config.MapHttpAttributeRoutes();

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

This makes "/api/Test/Test?id=1" work but not "/api/Test/Test/1"

I tried to add the attribute routing on the action, which makes "/api/Test/Test/1" work but now "/api/Test/Test?id=1" doesn't work:

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

回答1:


Working with the ASP.NET Web API template, I configured the WebApi routes like this:

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

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

You will need to be careful with the actions you include in your controller. For example, I have the ValuesController like this:

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

With the routes configured as shown above, the api will respond to the following requests:

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

However, things get complicated if you add a second HttpGet action in the same controller. The server, when trying to respond to the request that maps to the first route (DefaultApi), won't be able to figure out which get action to send. The DefaultApi route kind of relies on a convention that a controller will only have 1 action to service each kind of HTTP request. If you add a second HttpGet action named Try:

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

The server will respond as follows:

/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

If you intend to use more than 1 method for each type of HTTP request in the same controller, I recommend removing the DefaultApi route and keeping only the ActionApi. Another example:

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

Now, the server will handle the following requests successfully:

/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

However, these will not work:

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

Parameter isn't called "id" for the method "Play", the server doesn't know what you want. Response:

<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

The server expects what comes after the controller name, values in this case, to be a controller action. The 3 obviously isn't so the server doesn't know how to handle the request.

<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>

I hope this post gives you enough information to configure your API however you want.



来源:https://stackoverflow.com/questions/59157364/how-to-support-both-kinds-of-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!