WebApi路由个性化设置实例参考
作者:C/S框架网  发布日期:2017-09-23 14:30:46
  WebApi路由个性化设置实例参考





C# Code:

public static void RegisterRoutes(RouteCollection routes)
{
   
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   
   //预设配置
   routes.MapHttpRoute(
   name: "DefaultApi",
   routeTemplate: "api/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
   );
   
   //自定义配置-test-api
   routes.MapHttpRoute(
   name: "TestApi",
   routeTemplate: "test-api/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional, controller = "Test" }
   );
   
   //自定义配置-Action支持
   routes.MapHttpRoute(
   name: "ActionApi",
   routeTemplate: "action/{controller}/{action}/{id}",
   defaults: new { id = RouteParameter.Optional, controller = "Test" }//指定TestController
   );
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898





C# Code:

[RoutePrefix("test-api/test")]
public class TestController : ApiController
{
   // GET api/<controller>
   public IEnumerable<string> Get()
   {
      return new string[] { "Get-value1", "Get-value2" };
   }
   
   [HttpGet]
   [Route("v1/get1")]
   public IEnumerable<string> Get1()
   {
      return new string[] { "v1/get1---Value1", "v1/get1---Value2" };
   }
   
   [HttpGet]
   [ActionName("do")]
   public IEnumerable<string> DoAction(string id)
   {
      return new string[] { "v1/get1---Value1", "v1/get1---Value2", id };
   }
   
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:

[RoutePrefix("api/demo")]
public class DemoController : ApiController
{
   [HttpGet]
   [Route("get111")]
   public IEnumerable<string> Get111()
   {
      return new string[]
      {
         "CSFramework.WebAPI.Demo",
         "www.csframework.com",
         "www.cscode.net",
         "作者:孙中吕"
         };
      }
      
      [HttpGet]
      [Route("get1")]
      public string Get(int id)
      {
         return "CSFramework.WebAPI.Demo,GET返回结果:\r\n" id.ToString();
      }
      
      [HttpPost]
      [Route("post")]
      public string Post(dynamic modelObject)
      {
         return "CSFramework.WebAPI.Demo,Post返回结果:\r\n" modelObject.ToString();
      }
      
      [HttpGet]
      [Route("get2")]
      public string Get2(int id,int idd)
      {
         return "value";
      }
      
   }
   
   //来源:C/S框架网(www.csframework.com) QQ:1980854898
   


PostMan测试结果:


贴图图片

匹配路由

C# Code:

[HttpGet]
[Route("get111")]
public IEnumerable<string> Get111()
{
   return new string[]
   {
      "CSFramework.WebAPI.Demo",
      "www.csframework.com",
      "www.cscode.net",
      "作者:孙中吕"
      };
   }
   
   //来源:C/S框架网(www.csframework.com) QQ:1980854898
   


贴图图片


匹配路由

C# Code:

[HttpGet]
[ActionName("do")]
public IEnumerable<string> DoAction(string id)
{
   return new string[] { "v1/get1---Value1", "v1/get1---Value2", id };
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898





贴图图片


匹配路由

C# Code:

[HttpGet]
[ActionName("do")]
public IEnumerable<string> DoAction(string id)
{
   return new string[] { "v1/get1---Value1", "v1/get1---Value2", id };
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898




贴图图片

匹配路由


C# Code:

[HttpGet]
[Route("v1/get1")]
public IEnumerable<string> Get1()
{
   return new string[] { "v1/get1---Value1", "v1/get1---Value2" };
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇