C# ASP.NET无法解析xml!检查文件路径

C# ASP.NET无法解析xml!检查文件路径,c#,asp.net,asp.net-mvc,api,C#,Asp.net,Asp.net Mvc,Api,我通过ASP.NET MVC 4创建了一个简单的API: public class ActionController : ApiController { [WebMethod] public string getCommunities() { try { MethodClass method = new MethodClass(); return method.getCommunities()

我通过ASP.NET MVC 4创建了一个简单的API:

public class ActionController : ApiController
{
    [WebMethod]
    public string getCommunities()
    {
        try
        {
            MethodClass method = new MethodClass();
            return method.getCommunities();
        }
        catch(Exception ex)
        {
            return ex.Message.ToString();
        }
    }
}
正在尝试在method类中调用此方法:

public string getCommunities()
{
    return "bbb";
}
但无论出于什么原因,我都会犯这样的错误:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cannot parse xml! Check file path</string>
无法解析xml!检查文件路径

我试着用谷歌搜索错误,但什么也没找到,以前有人见过这个错误吗?我该如何修复它呢?

正如评论中已经指出的,您在错误的地方查找错误。方法.getCommunities()引发错误,消息为“无法解析xml!检查文件路径”

在我看来,谷歌搜索你的代码可能会抛出一个自定义异常:在代码中搜索该字符串可能会将你指向正确的位置

作为概念的快速证明,我更改了VisualStudioWebAPI模板生成的标准API

public string Get(int id)
        {
            try
            {
                var t = 0;
                var i = 1 / t;
                return "bbb";
            }
            catch { return "ABBA"; }
        }
它准确地以xml字符串形式返回自定义错误消息

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ABBA</string>
ABBA

我试图通过在ASP.Net MVC 4模板中创建简单的
ActionController.cs
来复制您提到的案例,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Services;

namespace MvcApiApplicationTrial1.Controllers
{
  public class ActionController : ApiController
  {
    [WebMethod]
    public string getCommunities() {
      try {
        MethodClass method = new MethodClass();
        return method.getCommunities();
      } catch (Exception ex) {
        return ex.Message.ToString();
      }
    }
  }

  public class MethodClass
  {
    public string getCommunities() {
      return "bbb";
    }
  }
}
并使用以下url在web浏览器(Chrome)中调用它:

http://localhost:56491/api/Action/getCommunities
并得到以下正确的结果:

如果声明、定义和调用正确,那么代码应该没有任何问题

因此,我建议您再次检查您的声明、定义以及对相关控制器/方法的调用。你的问题可能在别的地方

而且,由于该错误似乎是一个自定义错误,从单独发布的代码判断,问题可能出在
getCommunities
方法的某个地方。检查方法,尝试在那里找到“无法解析xml!”文本。或者,错误出现在
MethodClass
构造函数中,但可能性较小。同样,检查您的
MethodClass
,尝试查找“cannotparsexml!”文本

至于您在问题中发布的给定案例,我没有发现任何问题


但是
try
和“bbb”之间的任何其他内容也可能是产生错误的根源。如果
try
块中有更多内容,并且我不确定错误可能实际产生在何处,那么检查错误文本将是我的第一步。

在Global.asax.cs中应将代码放在下面:

GlobalConfiguration.Configuration.Formatters.Clear(); GlobalConfiguration.Configuration.Formatters.Add(新系统.Net.Http.Formatting.XmlMediaTypeFormatter())

并在下面的WebApiConfig代码中:

config.Formatters.XmlFormatter.SupportedMediaTypes.Add(新的MediaTypeHeaderValue(“text/xml”);
var appXmlType=config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t=>t.MediaType==“应用程序/xml”)

网络方法?为什么会在这里?因为我计划通过ajax使用它[WebMethod],MVC应用程序不需要它。页面方法是在代码隐藏(aspx.cs)页面中使用的老式方法……在
MethodClass
构造函数中发生了什么?我怀疑这可能是抛出
异常的地方…@user979331我注意到你在这个问题上悬赏了,但你不可能得到比你得到的更好的回答。编辑该问题以提供GetCommunities()的实现,您可能会得到答案!