我该如何配置路由 [英] How can I configure route

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

问题描述

我在WebApiConfig中有这三条路线,但如果{name}的同名{action}存在问题。如何配置路由以区分所有(名称,ID,动作,另一个对象..)



我尝试过:



I have this three route in my WebApiConfig, but there is a problem if there are an {action} in the same name of {name}. How can i configure the route to make the difference between all (name, id, action, another object..)

What I have tried:

config.Routes.MapHttpRoute(
                        name: "ApiByName",
                        routeTemplate: "{controller}/{name}",
                        defaults: null,
                        constraints: new { name = @"^[a-z]+$" }
                    );

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

推荐答案

}
);

config.Routes.MapHttpRoute(
name:< span class =code-string> DefaultApi
routeTemplate: {controller} / {id}
默认值: new {id = RouteParameter 。可选}
);
config.Routes.MapHttpRoute(
name: ApiByAction
routeTemplate: {controller} / {action});
" } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "ApiByAction", routeTemplate: "{controller}/{action}");


WebApiConfig 您可以根据需要设置多个路径,

在Web APi中,您可以拥有由MVC创建的标准 ApiController API控制器,它具有标准的Res​​t API方法,如下所示,其中我添加了一个Get方法,仅用于演示,输入参数为名称

In WebApiConfig You can set as many route as you need,
In web APi you can have standard ApiController created by MVC API controller which has standard Rest API methods like below, Where I have added one more Get method just for your demonstration with input parameter as name
public class TestController : ApiController
   {
       // GET: api/Test
       public IEnumerable<string> Get()
       {
           return new string[] { "value1", "value2" };
       }

       // GET: api/Test/5
       public string Get(int id)
       {
           return "value";
       }

       public string Get(string name)
       {
           return "value";
       }

       // POST: api/Test
       public void Post([FromBody]string value)
       {
       }

       // PUT: api/Test/5
       public void Put(int id, [FromBody]string value)
       {
       }

       // DELETE: api/Test/5
       public void Delete(int id)
       {
       }
   }



对于这些类型的控制器,您不需要操作,只需将路径设置为


For these type of controller you do not requires action and you can simply set a route as

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



在调用实际API时需要通过控制器/获取或发布或放置或删除

但是如果你有使用您自己的名称的显式API方法然后您必须将方法名称指定为操作。


while calling the actual API you need to pass the controller/ Get or Post or Put or Delete
but if you have explicit API methods with your own name then you have to specify the method name as action.

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



类似地,你可以根据需要再多一次溃败。


similarly you can have one more rout as you required.

config.Routes.MapHttpRoute(
                           name: "ApiByName",
                           routeTemplate: "{controller}/{action}/{name}",
                           defaults: null,
                           constraints: new { name = @"^[a-z]+


}
);



在所有情况下,你必须拥有非如果需要为自己的操作设置参数,则为空字符串值操作。如果您需要使用标准的Get,Post,Delete,Put方法,那么只能使用带有控制器和参数的多个路径。以下是标准和您自己的API操作的标准webApi.config文件


In all cases you must have non empty string value action if you need to set parameters for your own actions. If you need to use standard Get, Post, Delete, Put methods then can have multiple routs with controllers and parameters only. below is the standard webApi.config file for both standard and your own API actions

public static void Register(HttpConfiguration config)
      {
          // Web API configuration and services
          // Configure Web API to use only bearer token authentication.
          config.SuppressDefaultHostAuthentication();
          config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

          // Web API routes
          config.MapHttpAttributeRoutes();

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

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

          config.Routes.MapHttpRoute(
              name: "DefaultApiwithname",
              routeTemplate: "{controller}/{name}",
              defaults: null,
              constraints: new { name = @"^[a-z]+


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

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