• 在asp.net web api中动态修改action的名字


    在路由设置中,我的路由是这样的:

    /api/{controller}/jqGrid/{action}/{id}

    对于如下URL,默认情况下执行的是UserController类的List方法:

    /api/User/jqGrid/List

    而我希望凡是url中含有jqGrid的路由,都执行“jqGrid_{action}”名字的方法,即  jqGrid_List 方法。经过数天地折磨,终于解决了。上代码(这里照搬我在stackoverflow上的提问和我自己的回答了,英语高手欢迎指出文中不地道的英语,谢谢):

    First of all, I need to add a JqGridControllerConfiguration attribute to replace the default action selector applied to the controller with my one.

    [JqGridControllerConfiguration]
    public class UserController : ApiController
    {
        // GET: /api/User/jqGrid/List
        [HttpGet]
        public JqGridModel<User> jqGrid_List()
        {
            JqGridModel<User> result = new JqGridModel<User>();
            result.rows = Get();
            return result;
        }
    }

    Here's the code of JqGridControllerConfiguration:

    1 public class JqGridControllerConfiguration : Attribute, IControllerConfiguration
    2 {
    3     public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
    4     {
    5         controllerSettings.Services.Replace(typeof(IHttpActionSelector), new JqGridActionSelector());
    6     }
    7 }

    in JqGridActionSelector, the "action" is modified if a "jqGrid/" exists in the request URL.

     1 public class JqGridActionSelector : ApiControllerActionSelector
     2 {
     3     public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
     4     {
     5         Uri url = controllerContext.Request.RequestUri;
     6         if (url.Segments.Any(s => string.Compare(s, "jqGrid/", true) == 0))
     7         {
     8             controllerContext.RouteData.Values["action"] = "jqGrid_" + controllerContext.RouteData.Values["action"].ToString();
     9         }
    10 
    11         return base.SelectAction(controllerContext);
    12     }
    13 }

     

  • 相关阅读:
    [HDOJ1827]Summer Holiday(强连通分量,缩点)
    [CF676C]Vasya and String(尺取法,原题)
    [51NOD1105]第k大的数(二分答案)
    [51NOD]BSG白山极客挑战赛
    [HDOJ4635]Strongly connected(强连通分量,缩点)
    Miller_Rabin(米勒拉宾)素数测试算法
    hdu 3501 Calculation 2 欧拉函数
    codeforces 350 div2 D Magic Powder
    codeforces 350 div2 C. Cinema map标记
    hdu 2824 The Euler function 欧拉函数打表
  • 原文地址:https://www.cnblogs.com/Ricky81317/p/2678717.html
Copyright © 2020-2023  润新知