[英]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);
}
Consider using, just for this one problem, XML literals in VB9 rather than serialization. 考虑到这个问题,考虑在VB9中使用XML文字而不是序列化。 Seriously.
说真的 Just give it 20 minutes of your time.
只需花20分钟即可。 There's many options.
有很多选择。
http://www.hanselman.com/blog/TheWeeklySourceCode30VBNETWithXMLLiteralsAsAViewEngineForASPNETMVC.aspx http://www.hanselman.com/blog/TheWeeklySourceCode30VBNETWithXMLLiteralsAsAViewEngineForASPNETMVC.aspx
http://www.hanselman.com/blog/XLINQToXMLSupportInVB9.aspx http://www.hanselman.com/blog/XLINQToXMLSupportInVB9.aspx
http://blogs.msdn.com/dmitryr/archive/2008/12/29/asp-net-mvc-view-engine-using-vb-net-xml-literals.aspx http://blogs.msdn.com/dmitryr/archive/2008/12/29/asp-net-mvc-view-engine-using-vb-net-xml-literals.aspx
http://haacked.com/archive/2008/12/29/interesting-use-of-xml-literals-as-a-view-engine.aspx http://haacked.com/archive/2008/12/29/interesting-use-of-xml-literals-as-a-view-engine.aspx
http://www.infoq.com/news/2009/02/MVC-VB http://www.infoq.com/news/2009/02/MVC-VB
For what you're doing, returning XML as a poor-man's Web Service, this is tailor-made. 对于您正在做的事情,将XML作为穷人的Web服务返回,这是量身定制的。
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.