首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.net webforms应用程序中的主机集成WCF rest服务

.net webforms应用程序中的主机集成WCF rest服务
EN

Stack Overflow用户
提问于 2013-07-19 02:54:00
回答 2查看 988关注 0票数 1

我有一个传统的.net web应用程序,其中包含.aspx页面和隐藏文件的代码...

我想在UI上使用一些jquery,并希望使用json服务来连接来自jquery-easyui的网格之类的东西。我已经看到了一些不好的选择,比如使用IIS返回json内容类型和独立的.aspx示例,但我希望在IIS中托管服务,因为应用程序是在托管的在线提供商上。

最简单的方法是什么是最好的实践,我如何在VS2012和我的本地IIS托管站点以及生产internet站点中实现它?

EN

回答 2

Stack Overflow用户

发布于 2013-07-19 03:39:00

您可以使用Web.Api创建rest服务,并将其作为网站的一部分进行托管。请在这里查看完整的教程:http://www.asp.net/web-api/tutorials/hands-on-labs/build-restful-apis-with-aspnet-web-api

票数 0
EN

Stack Overflow用户

发布于 2013-07-20 01:40:48

您还可以将WCF服务直接包含到您的web应用程序中,并对其进行配置,使其能够返回和接收JSON。它们看起来像这样:

代码语言:javascript
复制
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class YourServiceDoesJSON
{        
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public ReturnTypeThatWillBeTransformedIntoJSON GetWhatever(string parameterIfYouNeed)
    {
        // do something
        return new ReturnTypeThatWillBeTransformedIntoJSON();
    }

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
    public SomethingElse PostWhatever()
    {
            ...
    }
}

此服务的配置文件如下所示:

代码语言:javascript
复制
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebApp.Folder.YourServiceDoesJSONAspNetAjaxBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MetadataBehaviors" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WebApp.Folder.YourServiceDoesJSON" behaviorConfiguration="MetadataBehaviors">

        <endpoint address="" behaviorConfiguration="WebApp.Folder.YourServiceDoesJSONAspNetAjaxBehavior"
          binding="webHttpBinding" contract="WebApp.Folder.YourServiceDoesJSON" />
      </service>
    </services>

注意:这就是我在知道将web应用程序转换为MVC是如此容易之前所做的;请参阅我之前的回答;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17731673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档