SORU
25 EYLÜL 2008, PERŞEMBE


XML denetleyicisi dönüş'in Eylem bir ActionResult olarak?

ASP.NET MVC denetleyicisi eylem XML dönmek için en iyi yolu nedir? JSON, ama XML için değil, dönmek için iyi bir yol yoktur. Gerçekten bir Görünümü aracılığıyla XML rota gerek var mı, ya da Cevap değil-en pratik şekilde yapmalıyım.-Ing üzeresiniz.

CEVAP
25 EYLÜL 2008, PERŞEMBE


Kullanım MVCContrib'In XmlResult Eylem.

Başvuru için buraya kodlarını

public class XmlResult : ActionResult
{
    private object objectToSerialize;

    /// <summary>
    /// Initializes a new instance of the <see cref="XmlResult"/> class.
    /// </summary>
    /// <param name="objectToSerialize">The object to serialize to XML.</param>
    public XmlResult(object objectToSerialize)
    {
        this.objectToSerialize = objectToSerialize;
    }

    /// <summary>
    /// Gets the object to be serialized to XML.
    /// </summary>
    public object ObjectToSerialize
    {
        get { return this.objectToSerialize; }
    }

    /// <summary>
    /// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
    /// </summary>
    /// <param name="context">The controller context for the current request.</param>
    public override void ExecuteResult(ControllerContext context)
    {
        if (this.objectToSerialize != null)
        {
            context.HttpContext.Response.Clear();
            var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
            context.HttpContext.Response.ContentType = "text/xml";
            xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
        }
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • kruno j

    kruno j

    6 Mayıs 2007
  • Rachel Raum

    Rachel Raum

    10 EYLÜL 2007
  • Wild Academy

    Wild Academy

    8 Aralık 2009