使用RESTful服务返回数据 [英] Return data with RESTful services

查看:119
本文介绍了使用RESTful服务返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在WCF中玩RESTful Web服务.我正在Visual Studio中使用名为"WCF REST服务模板40(CS)"的在线模板.

无论如何,一切都很好,我的服务调用返回了适当的XML数据(这就是我想要返回的数据)

刚开始,我非常喜欢返回一个类很容易,并且可以将其自动转换为XML.一个简单的示例是返回一个字符串,该字符串的值表示身份验证令牌,返回的XML为:

Hi All,

I am playing around with RESTful web servcies in WCF. I am using the online template in visual studio called ''WCF REST Service Template 40(CS)''.

Anyway, all is good and my service calls return appropriate XML data (which is what I am looking to return)

At first, I quite liked how easy it was to return a class and it would automatically be translated into XML. A simple example would be return a string with a value representing an authentication token, the XML returned would be:

<string>6fa1b98d-7bf9-43a7-ad26-8c3ae382a198</string>



...非常简单!但是现在,我正在决定我真正想要返回的东西是...



...easy as pie! But now, I am deciding what I really want to return is something like...

<token>6fa1b98d-7bf9-43a7-ad26-8c3ae382a198</token>



...所以我的最终问题是,实现这一目标的最佳方法是什么?

如果我有一个名为"token"的自定义类,并且我覆盖了处理该函数中"to xml"部分的任何函数,我可能会工作,但是这可能会过大,而且看起来似乎不正确

我真正想做的是使用ASP.Net MVC框架包含一个视图".因此,我可以像在MVC 3中一样轻松地在View中定义XML格式...



...So my ultimate question is, what is the best way to make this happen?

I though it would probably work if I had a custom class called ''token'' and I override whatever function handles the ''to xml'' part of it - but this would be overkill and just doesn''t seem right

What I would really like to be able to do, is to include a ''View'' as would be possible using the ASP.Net MVC framework. So I could define my XML format in a View as easy as (in MVC 3)...

<token>
    @ViewBag.Token
</token>



...在WCF中可能吗?也许我应该使用MVC 3而不是WCF来进行RESTful服务?有什么想法吗?

感谢



...is this possible in WCF? Perhaps I should use MVC 3 to do the RESTful service instead of WCF? Any thoughts?

Thanks

推荐答案

好吧,我一直在研究这个问题,到目前为止我发现的内容如下.

为了返回完全符合我想要的格式的XML,我找到了
Well I have been looking into this and what I have found so far is as follows.

In order to return XML formatted exactly how I wanted it, I found this post[^] which shows how to convert an XMLDocument into a Message. Then with a further helper class my calling code can now be as simple as this...

[WebGet(UriTemplate = "Test")]
public Message Test()
{
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.LoadXml("<token>1234</token>");

    return CustomMessage.XML(doc);
}



...产生我一直在寻找的XML结果.我还能够使用一些Regex和Reflection来生成MVC样式视图解析器,以解析XML文件中的变量,并用它们替换给定类实例中的匹配参数...



...which produces the XML result I was looking for. I was also able to produce an MVC style view parser using a bit of Regex and Reflection to parse for variables in the XML file and substitute them for matching parameters in a given class instance...

public static Message XML(string view, object model)
{
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(System.Web.Hosting.HostingEnvironment.MapPath("~/Views/" + view + ".xml"));

    return new CustomResponses().CreateResponseMessage(doc, model);
}





public System.Xml.XmlDocument MergeDoc(System.Xml.XmlDocument doc, object model)
{
    string xml = doc.OuterXml;
    System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties();
    foreach (Match match in Regex.Matches(xml, "@ViewBag.[a-zA-Z0-9]{1,}", RegexOptions.None))
    {
         string propertyName = match.Value.Replace("@ViewBag.", "");
         System.Reflection.PropertyInfo property = model.GetType().GetProperty(propertyName);
         xml = xml.Replace(match.Value, property.GetValue(model, null).ToString());
    }
    doc.LoadXml(xml);
    return doc;
}



以上两个功能允许一个包含以下内容的xml文件:



These two above functions allow for an xml file containing:

<token>@ViewBag.Token</token>



可以与类实例合并并使用,例如:



to be merged with a class instance and used like:

public class TokenClass
{
    public string Token { get; set; }
}

[WebGet(UriTemplate = "Test2")]
public Message Test2()
{
    TokenClass token = new TokenClass() { Token = "123456" };

    return CustomMessage.XML("Token", token);//params here are View and Model
}



返回的XML响应将是...



the returned XML response would be...

<token>123456</token>



...这不是100%理想的,因为您没有解析xml文件中代码的额外功能(例如for循环),而MVC 3中使用的Razor视图引擎可能无法实现此功能(有一个
在此处发布 [此处 [



...this is not 100% ideal as you do not have the extra functionality of parsing code within the xml file (such as a for loop) that would be possible with the Razor view engine used in MVC 3 (there is a post here[^] that shows how the Razor engine can be used outside of MVC 3 that may interest some but I am not sure it is what I want so I may chose to leave it out)

Anyway, I will probably continue to investigate further possibilities but hopefully this information will be of use to someone else. Also, if anyone reading this has any further comments, I would be interested in hearing them



Here[^] is a nice little article on how to use the Razor view engine without MVC and I can say that it works pretty well


这篇关于使用RESTful服务返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆