ASP.NET cannot parse xml! Check file path

I created a simple API through ASP.NET MVC 4:

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

which is trying to call this method in the Method class:

public string getCommunities()
{
    return "bbb";
}

but for some reason I get this error:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cannot parse xml! Check file path</string>

I tried Googling for an error, but didn't come up with anything, has anyone seen this error before? and how to fix it?

+4
source share
3 answers

As already mentioned in the comments, you are looking for an error in the wrong place. method.getCommunities () throws an error with the message "cannot parse xml! Check file path".

Google , : .

API, -API Visual Studio.

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>
+5

, ActionController.cs ASP.Net MVC 4 :

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";
    }
  }
}

- (Chrome) URL:

http://localhost:56491/api/Action/getCommunities

:

enter image description here

, , .

, , , /. .

, -, , , , - getCommunities. , " xml!". . , , MethodClass. , MethodClass, " xml!". .

, , .

try "bbb" . , try , , .

+2

in Global.asax.cs you should add the code below:

GlobalConfiguration.Configuration.Formatters.Clear (); GlobalConfiguration.Configuration.Formatters.Add (new System.Net.Http.Formatting.XmlMediaTypeFormatter ());

and in the text below of WebApiConfig:

config.Formatters.XmlFormatter.SupportedMediaTypes.Add (new MediaTypeHeaderValue ("text / xml")); var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault (t => t.MediaType == "application / xml");

0
source

Source: https://habr.com/ru/post/1628535/


All Articles