从wcf服务返回html [英] return html from wcf service
问题描述
我有一个web服务,我需要返回一个包含html的字符串。该html是Select控件的标记(用于jqGrid搜索过滤器),例如
< select>< option id ='1'>值1< / option>< / select>
我的WCF Web服务包含一个将该值作为字符串返回的方法...
public string GetLeadTypeSelect()
{
return< select>< option id ='1'>值1< / option>< / select>;
}
此方法的合约为:
[OperationContract]
[WebInvoke(Method =GET,
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat。 Json)]
string GetLeadTypeSelect();
我的问题是转义字符被插入到字符串中,因此呈现返回的HTML无用 - 服务返回这:
< select>< option id ='1'> value 1< \ / option> < \ /选择>中
引号和转义'/'
在结束时< option>
和< select>
标签都会导致问题。
jqGrid使用返回的HTML显示下拉菜单...
filterModel:[
{label:'Type',name:'type',stype:'select',surl:'../../../Services/Leads/GetLeads.svc/GetLeadTypeSelect'},
所以,我的问题是,如何从此Web服务将纯HTML返回给客户端,以便它可以插入进入我的HTML页面?
在此先感谢您的任何帮助,
Colin。
我知道这是一个旧帖子,但对于只能在IIS中托管的服务,这是一种简单的方法来返回html:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public Stream getHtml()
{
//获取html
var html = DoSomethingToGetHtml(); //不是内置的.Net方法;)
//我们希望我们的结果解释为纯html
WebOperationContext.Current.OutgoingResponse.ContentType =text / html; charset = utf -8\" ;
//从我们的html中创建一个流,因为尝试返回一个字符串会为响应添加一个额外的头标记
//。返回一个流自身返回html
var result = new MemoryStream(Encoding.UTF8.GetBytes(html));
//返回结果
返回结果;
}
此代码在托管服务的web.config中需要以下内容:
< system.serviceModel>
<! - 通过设置aspNetCompatibilityEnabled = true - >来启用HttpContext.Current。
< serviceHostingEnvironment aspNetCompatibilityEnabled =true/>
<行为>
< endpointBehaviors>
< behavior name =webHttpEnabled>
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>
...
< system.serviceModel>
在服务配置中,设置 以这种方式返回html会限制服务的可重用性,但如果您确信自己确实可以轻松解决问题该服务将始终托管在IIS中。 I have a web service from which I need to return a string containing html. This html is the markup for a Select control (for use in jqGrid search filters), e.g. My WCF web service contains a method that returns this value as a string... and the contract for this method is : My problem is that escape characters are inserted into the string so rendering the returned HTML useless - the service returns this : The quotation marks and the escaped jqGrid uses the returned HTML to display the dropdown... So, my question is, how to I return pure HTML back to the client from this web service so that it can be inserted into my HTML page? Thanks in advance for any help,
Colin. I know this is an old post, but for a service that will only be hosted in IIS, here is an easy way to return html: This code requires the following in the web.config hosting the service: In the configuration for the service, set Returning html in this way limits the reusability of the service a bit, but it's an easy way to solve the problem if you're reasonably sure the service will always be hosted in IIS. 这篇关于从wcf服务返回html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! behaviorConfiguration =webHttpEnabled
<select><option id='1'> value 1 </option></select>
public string GetLeadTypeSelect()
{
return "<select><option id='1'> value 1 </option></select>";
}
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json)]
string GetLeadTypeSelect();
"<select><option id='1'> value 1 <\/option><\/select>"
'/'
in the closing <option>
and <select>
tags both cause problems.filterModel: [
{ label: 'Type', name: 'type', stype: 'select', surl: '../../../Services/Leads/GetLeads.svc/GetLeadTypeSelect' },
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public Stream getHtml()
{
// get the html
var html = DoSomethingToGetHtml(); //Not a built-in .Net method ;)
// we want our result interpreted as plain html
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html; charset=utf-8";
// create a stream from our html because trying to return a string adds an extra header tag
// to the response. Returning a stream returns the html by itself
var result = new MemoryStream(Encoding.UTF8.GetBytes(html));
// return the result
return result;
}
<system.serviceModel>
<!-- enable HttpContext.Current by setting aspNetCompatibilityEnabled=true-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpEnabled">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
...
<system.serviceModel>
behaviorConfiguration="webHttpEnabled"
.