4

I created a simple API via 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 whatever 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 the error, but came up with nothing, has anyone seen this error before? and how do I fix it?

12
  • 1
    WebMethod? Why is it here? Commented Feb 12, 2016 at 16:13
  • because I am planning on using this via ajax Commented Feb 12, 2016 at 16:47
  • [WebMethod] is not require for MVC applications. Page Methods are old school way of doing in code behind (aspx.cs) pages... Commented Feb 12, 2016 at 17:45
  • 1
    What's happening in the MethodClass constructor? I suspect that might be where the Exception is thrown... Commented Feb 16, 2016 at 8:14
  • 4
    @user979331 I noticed you put a bounty on the question but it's unlikely you receive any better responses than the one you have. Edit the question in order to provide the implementation of GetCommunities() and you might get an answer! Commented Feb 19, 2016 at 17:06

3 Answers 3

5
+100

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

Googling your error it seems to me that you are throwing a custom exception: searching for that string in your code may point you to the right place.

As a quick proof of concept I changed the standard API generated by Visual Studio Web API template.

public string Get(int id)
        {
            try
            {
                var t = 0;
                var i = 1 / t;
                return "bbb";
            }
            catch { return "ABBA"; }
        }

which exactly returns the custom error message as xml string

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ABBA</string>
Sign up to request clarification or add additional context in comments.

Comments

2

I attempted to replicate the case you mention by creating simple ActionController.cs in ASP.Net MVC 4 template as follow:

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

And call it in the web browser (Chrome) with the following url:

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

And get the following correct result:

enter image description here

If you declare, define, and call things right, your code should have no problem at all.

So, I suggest you to re-check your declaration, definition, as well as your calling to the related Controller/Method again. Your problem may lay somewhere else.

And since the error seems to be a custom error, judging from the code posted alone, likely that the problem lays somewhere in your getCommunities method. Check the method, try to find the "cannot parse xml!" text there. Alternatively, but less likely, the error is in the MethodClass constructor. Same thing, check your MethodClass, try to find the "cannot parse xml!" text.

As for the given case as what you have posted in your question, I found no issue at all.

But anything else in between try and "bbb" can also potentially be the source of the created error. Checking the error text would be my first step if there are more things in the try block and I am unsure where the error may actually be generated.

Comments

0

in Global.asax.cs should put code bellow:

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

and in WebApiConfig code bellow:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.