设置的WebAPI 2路 [英] Set WebApi 2 routes

查看:180
本文介绍了设置的WebAPI 2路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的WepApi2和创建REST风格web服务,但我得到的大部分的窍门。

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

最近,我开始开发一个新的WebApi2在那里我遇到了一些我的查询需要一些unesacapable人物,特别是斜线和倒斜线的。

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.

我在这里读了几教程和提问此事,但没有就足够了我,所以我结束了在设置我的API查询像这样

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

一切都运行完美。我不得不设置了 ActionName 路线属性我的行动,也没有问题,但一旦我试过的老办法查询

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

我总是会得到一个没有采取任何控制器控制器与请求匹配上找到。

下面是我的路线配置

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

您可能会注意到我有2配置。好,首先是一个控制器下的动作进行分类,如这样,在将来,可以与特定的操作加,而不是具有一个特定动作的巨大的控制器列表多个控制器,但第二种方法是客户端想要什么

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

编辑:我不好,我忘了如何展示我的控制器和动作,其中设置

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

正如你所看到的,除了在配置设置的路线,我还设置操作路由属性,但我不知道我可以设置1个多路由属性。会尝试,并报告!

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!

推荐答案

您可以尝试使用<一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2\"相对=nofollow>属性的路由,而不是:

// 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)
    {
        // ...
    }
}

这篇关于设置的WebAPI 2路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆