none
webapi 如何给delete接口传递多个参数 RRS feed

  • 问题

  •        刚开始网上有使用这种方式的DeleteRoleRightsInfo(dynamic data),使用dynamic,但是前台传递数据过来data就是个object,啥都没有,以下是前端要传递的数据data: { roleId: 20, rightId:20},我用了这种方式也是取不到值DeleteRoleRightsInfo(int roleId,int rightId),请问有什么好的办法,当然可以直接定义个实体包含这两个字段,但是感觉那样不可取.
    2016年5月18日 14:07

答案

  • 你好,

    你可以试试下面的代码,Client:

    HttpClient client = new HttpClient();
    
    Contact contact = new Contact {
        Name = "Henrik",
        Age = 100
    };
    
    // Post contact
    Uri address = new Uri(_baseAddress, "/api/contact");
    HttpResponseMessage response = await client.PostAsJsonAsync(address.ToString(), contact);
    
    // Check that response was successful or throw exception
    response.EnsureSuccessStatusCode();
    
    // Read result as Contact
    Contact result = await response.Content.ReadAsAsync<Contact>();
    
    Console.WriteLine("Result: Name: {0} Age: {1}", result.Name, result.Age);
    
    API:
    public class ContactController : ApiController
    {
        public dynamic Post(dynamic  contact)
        {
            return contact;
        }
    }


    你可以参考下面的详细资料:
    Creating a Web API Controller with dynamic type:
    http://dotnetthoughts.net/creating-a-web-api-controller-with-dynamic-type/

    希望能够帮到你。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月24日 6:21

全部回复

  • 我一般是用JSON格式,接口只包含一个字符串,字符串的内容则依据实际需要定义和解析


    专注于.NET MIS开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年5月18日 15:05
  • DeleteRoleRightsInfo(int roleId,int rightId) ,参数用queryString的方式传递。要么就定义实体
    2016年5月19日 2:44
  • 定义实体类吧,post也是这样的,如果参数多余一个,必须定义实体类
    2016年5月20日 0:05
  • 你好,

    你可以试试下面的代码,Client:

    HttpClient client = new HttpClient();
    
    Contact contact = new Contact {
        Name = "Henrik",
        Age = 100
    };
    
    // Post contact
    Uri address = new Uri(_baseAddress, "/api/contact");
    HttpResponseMessage response = await client.PostAsJsonAsync(address.ToString(), contact);
    
    // Check that response was successful or throw exception
    response.EnsureSuccessStatusCode();
    
    // Read result as Contact
    Contact result = await response.Content.ReadAsAsync<Contact>();
    
    Console.WriteLine("Result: Name: {0} Age: {1}", result.Name, result.Age);
    
    API:
    public class ContactController : ApiController
    {
        public dynamic Post(dynamic  contact)
        {
            return contact;
        }
    }


    你可以参考下面的详细资料:
    Creating a Web API Controller with dynamic type:
    http://dotnetthoughts.net/creating-a-web-api-controller-with-dynamic-type/

    希望能够帮到你。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月24日 6:21
  • 因为客户端调用是用augular来调用的,所以找到了另一种处理方式Newtonsoft.Json中的jobject
    2016年5月31日 1:50
  • 定义实体类
    2016年5月31日 2:54