从IHttpActionResult返回自定义XML响应

时间:2017-02-26 17:02:28

标签: c# xml web-services asp.net-web-api asp.net-web-api2

我的控制器中有以下代码

    [HttpGet]
    public IHttpActionResult Get_User_Info(long id)
    {
        user u = new user();
        List<user> lst = u.Get_userInfo(id);
        if (lst.Count==0)
        {
            return NotFound();
        }
        else
        return Ok(lst);
    }

和我的用户类是

public class user : NK.Objects._user
{

    public List<user> Get_userInfo(long userid)
    {
        address_city ac = new address_city();
        string sql = "select id ,username ,cgroup ,regDate ,name ,picadr ,tell ,caddresscity ,resadress ,email from " + tbl_name + " where id=@id";
        Q.dataaccesslayer.cmd.Parameters.Add("@id", System.Data.SqlDbType.BigInt).Value = userid;
        List<user> lst = Q.Execute_List<user>(sql, this, new Queries.Cmd_Parameters(), 10);
        lst.ForEach(t => t.resadress = ac.Get_City_Info(t.caddresscity) + " , " + t.resadress);
        return lst;
    }
}

用户类继承自以下_ 用户

public class _user
{
    public static string tbl_name = "tbl_user";
    public long id { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public long cgroup { get; set; }
    public string regDate { get; set; }
    public string name { get; set; }
    public string picadr { get; set; }
    public string tell { get; set; }
    public long caddresscity { get; set; }
    public string resadress { get; set; }
    public string email { get; set; }
}

xml返回

<caddresscity xmlns="http://schemas.datacontract.org/2004/07/NK.Objects">1</caddresscity>
<cgroup xmlns="http://schemas.datacontract.org/2004/07/NK.Objects">1</cgroup>
<email xmlns="http://schemas.datacontract.org/2004/07/NK.Objects">1</email>
<id xmlns="http://schemas.datacontract.org/2004/07/NK.Objects">1</id>
<name xmlns="http://schemas.datacontract.org/2004/07/NK.Objects">1</name>

我认为由于继承,我面临这个问题,当我没有继承时,它正如预期的那样工作。

如何将响应更改为如下所示:

<name>1</name>

0 个答案:

没有答案