简体   繁体   English

限制控制器返回的数据

[英]Limiting the data returned by a controller

I need advice on how to return a limited set of data from an MVC controller. 我需要有关如何从MVC控制器返回有限数据集的建议。

Lets say I have a class that is constructed like so: 可以说我有一个这样构造的类:

public interface ICustomerExpose
{
   string Name {get; set;}
   string State {get; set;}
}

public interface ICustomer: ICustomerExpose
{
   int Id {get; set;}
   string SSN {get; set;}
}

public class Customer: ICustomer
{
   ...
}

In my MVC project I have a controller action that returns customer data. 在我的MVC项目中,我有一个控制器操作来返回客户数据。 The project is actually more like a web service as there is no View associated with the data... we use the XmlResult (provided by the MVCContrib project). 该项目实际上更像一个Web服务,因为没有与数据关联的View ...我们使用XmlResult(由MVCContrib项目提供)。 The controller action looks like this: 控制器动作如下所示:

    // GET: /Customer/Show/5
    public ActionResult Show(int id)
    {
        Customer customer = Customer.Load(id);

        ...  // some validation work

        return new XmlResult((ICustomerExpose)customer);
    }

The above controller code does not work like I want it to. 上面的控制器代码无法正常运行。 What I want to happen is that only the Name and State properties are serialized and returned in the XmlResult. 我想发生的是,只有Name和State属性被序列化并在XmlResult中返回。 In practice the whole customer object is serialized including the data I definitely don't want exposed. 实际上,整个客户对象都是序列化的,包括我绝对不想公开的数据。

I know the reason this doesn't work: you can't serialize an interface. 我知道这行不通的原因:您无法序列化接口。

One idea floated around the office was to simply mark the properties Name and State as [XmlIgnore]. 在办公室里流传着一个想法,就是简单地将名称和状态属性标记为[XmlIgnore]。 However, this doesn't seem like a good solution to me. 但是,这对我来说似乎不是一个好的解决方案。 There might be other instances where I want to serialize those properties and marking the properties on the class this way prohibits me. 在其他情况下,我想序列化这些属性并以这种方式在类上标记这些属性禁止了我。

What is the best way to achieve my goal of only serializing the properties in the ICustomerExpose interface? 实现我仅对ICustomerExpose接口中的属性进行序列化的目标的最佳方法是什么?

Addendum: 附录:

For those interested in what XmlResult does here are the relevant parts of it: 对于那些对XmlResult的功能感兴趣的人,它是其中的相关部分:

 public class XmlResult : ActionResult
 {
     private object _objectToSerialize;
     public XmlResult(object objectToSerialize)
     {
        _objectToSerialize = objectToSerialize;
     }

     /// <summary>
     /// Serialises the object that was passed into the constructor 
     /// to XML and writes the corresponding XML to the result stream.
     /// </summary>
     public override void ExecuteResult(ControllerContext context)
     {
         if (_objectToSerialize != null)
         {
            var xs = new XmlSerializer(_objectToSerialize.GetType());
            context.HttpContext.Response.ContentType = "text/xml";
            xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize);
         }
     }
 }

您可以尝试这样做,但是我不确定它是否可以与xml序列化程序一起使用:

return new XmlResult(new { customer.Name, customer.State });

See this related question which recommends using an anonymous type. 请参阅此相关问题该问题建议使用匿名类型。

// GET: /Customer/Show/5
public ActionResult Show(int id)
{
    Customer customer = Customer.Load(id);

    ...  // some validation work

    var result = from c in cusomter
                 select new
                 {
                     Name = c.Name,
                     State = c.State,
                 };

    // or just

    var result = new
                 {
                     Name = customer.Name,
                     State = customer.State,
                 };


    return new XmlResult(result);
}

I ended up just doing the XmlIgnore as co-workers suggested, even though this left me with some undesirable (or so I thought) behaviors. 我最终只是按照同事的建议进行了XmlIgnore,尽管这使我产生了一些不良(或我认为)的行为。

To get around the fact that XmlIgnore would continue hiding properties that I might want serialized later I asked another question trying to find a way to around that issue. 为了避免XmlIgnore会继续隐藏以后可能需要序列化的属性这一事实,我问了另一个问题,试图找到解决该问题的方法。 Cheeso came up with a great answer making the XmlIgnore the best route (in my opinion) to take. Cheeso提出了一个很好的答案,使XmlIgnore成为最佳选择(我认为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 限制 LDAP 查询中返回的属性 - Limiting the attributes returned in an LDAP query 将模式附加到从控制器返回的Json数据 - Append Schema to Json data returned from controller 视图未显示MVC中控制器返回的数据 - View is not displaying returned data from controller in MVC 在从控制器返回的视图中显示数据 - Displaying data in a view returned from a controller .NET Core 2.2 Web API 中简单/基本的页码编号和限制返回数据的大小 - Easy/basic page numbering and limiting the size of returned data in the .NET Core 2.2 Web API linq to sql LoadWith返回限制字段 - linq to sql LoadWith limiting fields returned MVC controller 返回参数为复杂数据类型的 null - MVC controller returned parameter as null for complex data type SQL Server 2008 EF 4-限制使用权限返回的数据库记录? - SQL Server 2008 EF 4 - limiting database records returned using permissions? 问题循环从 ASP.NET MVC 控制器返回的数据 - Issue looping through data returned from an ASP.NET MVC controller Ember.js寻找从webapi控制器返回的第一个数据属性的模型 - Ember.js looks for model of first data property returned from webapi controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM