Set WebApi 2 routes

2019-09-05 08:10发布

问题:

I'm fairly new to WepApi2 and creating restfull webservice, but I get the hang of most of it.

Recently, I started developing a new WebApi2 where I encounter that some of my queries required some unesacapable characters, specially the slash and inverted slash ones.

I read several tutorials and questions here about the matter, but none would suffice me, so I ended up to setting my api queries like this

http://host/controler/action/?param1=x&param2=y

Everything works perfect. I had to setup the ActionName and Route attributes to my action, no problem there, but once I tried the old way of query

http://host/controller/action/x/y

I would always get a No action was found on the controller 'Controller' that matches the request.

Here's my routes configuration

// Web API configuration and services
            config.MapHttpAttributeRoutes();
            // Web API routes
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { controller = "HelloWorld", id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                name: "Config1",
                routeTemplate: "{controller}/{action}/{param1}/{param2}/{param3}",
                defaults: new { controller = "Controller", action = "action", param2 = RouteParameter.Optional, param3 = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                name: "Config2",
                routeTemplate: "{controller}/{param1}/{param2}/{param3}",
                defaults: new { controller = "action", param3 = RouteParameter.Optional }
            );

You may notice I have 2 configurations. Well, the first is to classify the action under a controller, as so, in the future, can add more controllers with specific actions, instead of having a huge list of controllers with an specific action, but the second approach is what the client wants.

So, is there a way to have both ways working together??

Edit: My bad, I forgot to show how my controller and actions where setup

public class ControllerController : WebApiController
    {
        [Route("Controller/action1/")]
        [ActionName("action1")]
        [HttpGet()]
        public Object action1(string param1, string param2)
        {
            // do action actions code
            return result;
        }

As you can see, besides setting the route in the config, I'm also setting the action routing attribute, but I wasn't aware I could set more than 1 Route attribute. Will try that and report back!

回答1:

You could try using attribute routing instead:

// in your startup configuration:

config.MapHttpAttributeRoutes();

// and your controller:

[RoutePrefix("foo")]
public class FooController
{
    [HttpGet]
    [Route("bar/{param1}/{param2}")]
    [Route("bar")
    public IHttpActionResult GetBar(string param1, string param2)
    {
        // ...
    }
}