C# 让WebService只返回纯文本JSON?

C# 让WebService只返回纯文本JSON?,c#,asp.net,json,web-services,C#,Asp.net,Json,Web Services,我正在尝试使用web服务返回FullCallendar资源正在请求的资源的json列表: 以下是web服务类: /// <summary> /// Summary description for CalendarServices /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)

我正在尝试使用web服务返回FullCallendar资源正在请求的资源的json列表:

以下是web服务类:

/// <summary>
/// Summary description for CalendarServices
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 CalendarServices : System.Web.Services.WebService
{
    public CalendarServices()
    {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetEmployees()
    {

        List<object> eventList = new List<object>();
        var emps = ResourceManager.GetAllEmployees();
        foreach (Employee e in emps)
        {
            eventList.Add(
           new
           {
               id = e.EmployeID.ToString(),
               name = e.EmployeName
           }
       );

        }



        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(eventList);
        return strJSON;
    }
}
我不知道这为什么不起作用。也许我不了解web服务

在这个例子中,他们调用resources.php,这只是对它的回应

基本上,我想要的是如果我转到CalendarServices.asmx/GetEmployees

我想在浏览器中看到以下内容:

[
{
"name":"Resource 2",
"id":"resource2"
},
{
"name":"Resource 1",
"id":"resource1"}
]
纯文本就行了。当前,如果我在浏览器中尝试此url,它将崩溃

我能做什么

谢谢

坠机事件:

Request format is unrecognized for URL unexpectedly ending in '/GetEmployees'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/GetEmployees'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/GetEmployees'.]
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +489333
   System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
   System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +226
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +145
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

您可以在fiddler中检查您的请求类型是POST还是GET,您可能需要在代码中添加一个额外的位。即

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]

那次崩溃是否伴随着一个行号,或者是一条可能提示问题所在的错误消息?ASMX是一项遗留技术,不应用于新的开发。WCF应用于web服务客户端和服务器的所有新开发。一个提示:Microsoft已停用MSDN上的。您是否尝试取消注释该行,以在AJAX调用中使用它?
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]