【问题标题】:hosintg WCF rest services in .net webforms app在 .net webforms 应用程序中托管 WCF 休息服务
【发布时间】:2013-07-18 18:53:59
【问题描述】:

我有一个带有 .aspx 页面和代码隐藏文件的传统 .net Web 应用程序...

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

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

【问题讨论】:

    标签: asp.net json wcf rest


    【解决方案1】:

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

    【讨论】:

    • 我没有使用 MVC,所以我认为这已经过时了。
    • 您可以轻松地将您的 Web 应用程序变成一个 mvc 超级。几个月前,我这样做是为了将 web.API 用于一个巨大的网络应用程序(1640 个工作日)。不到一天就完成了:我用 mvc 创建了一个新项目,像以前一样从原始 Web 应用程序中复制了所有代码(相同的文件夹,每个等),忽略了以 '.aspx' 结尾的路由,瞧,我可以使用 Web API。
    【解决方案2】:

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

    [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()
        {
                ...
        }
    }
    

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

    <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 如此简单之前,我就是这样做的;看我之前的回答;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-05
      • 2012-11-29
      • 2017-01-20
      • 2016-08-14
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多