2015-12-07 108 views
0

所以我一直在使用.asmx文件从Razor的c#类调用一个方法,并且我已经了解到.asmx已被弃用。我试图找出用什么来代替这个方法。下面是我现在在做什么:从.asmx升级到剃刀中的WCF

的C#程序(GetConstantContactData.cs)看起来是这样的:

using System.Net; 
using System.IO; 
using System.Runtime.Serialization.Json; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 




/// <summary> 
/// Summary description for GetConstantContactData 
/// </summary> 
[WebService(Namespace = "https://[myurl].org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class GetConstantContactData : System.Web.Services.WebService { 


public string GetResponse() 
    { 
     //Get data from Constant Contact and return it as a JSON string 
    } 

.asmx文件(GetConstantContactData.asmx)看起来是这样的:

<%@ WebService Language="C#" CodeBehind="~/App_Code/GetConstantContactData.cs" Class="GetConstantContactData" %> 

和JavaScript Ajax调用.asmx文件是这样的:

$.ajax({ 
type: "POST", 
contentType: "application/json; charset=utf-8", 
url: "../GetConstantContactData.asmx/GetResponse", 
data: "{}", 
success: SucceededCallback, 
error: FailedCallback, 
dataType: "json" 
}) 
// Callback function that 
// processes the service return value. 
function SucceededCallback(result) { 
    //do stuff with the data 
} 

它现在,但正如我所说我知道这种方法已被弃用,并不能在.NET 4中工作。据我所知,我想通过使用ServiceHost的WCF来完成此操作。我看到如何使用ScriptManager here来做到这一点,但我想知道如何使用Razor来做到这一点。

我一直在网上寻找任何类型的教程或指导。只是一个链接到正确的地方告诉我如何做到这一点会很好。谢谢。

+1

您是否将服务文件添加到现有的asp.net Web服务中,或者您是否正在创建WCF项目?我只是保留它,如果它目前与旧的asmx – Anonymous

+0

工作我想保留它,但我需要切换到.NET版本4,它不包括WebService。 – jimboweb

+0

WCF将具有一点学习曲线。您的网站和网络服务是否结合在一起?我肯定会把它们作为两个单独的项目。看看这里:http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery – Anonymous

回答

0

Brent Mannering对上述WebAPI端点的建议是解决此问题的最佳答案。它不使用WCF,但Web API是这个问题的一个更实用的轻量级解决方案,它与我最初使用.asmx Webservice框架进行的操作相匹配,并且不会被弃用。