C # Rest API return string with double quotes

I have this stupid problem and I hope someone can help enlighten me. I am creating an extension for Umbraco7 backoffice because I need to get a simple string. My problem is returning a string from a REST api containing double quotes and then an AngularJS wont model binding. Here's my API method:

public String GetWeek()
{
        var datetime = DateTime.Now;
        var cultureInfo = new CultureInfo("da-DK");
        var calendar = cultureInfo.Calendar;

        var week = calendar.GetWeekOfYear(datetime, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
        return datetime.Year + "-W" + week;
}

      

If someone can explain how I will get rid of these double quotes, I would be very grateful :)

Result:

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

      

+3


source to share


2 answers


I found a different way than @basarat suggests from @ArghyaC's comment form. The problem is that Umbraco REST controller is built on asp.net ControllerApi and has xml by default. The apostrophes come from xml serialization. The solution is just the power of json as a return value. I don't dare change this globally, but I can get the method to return with a JsonResult return value:

public JsonResult GetWeek()
    {
        var datetime = DateTime.Now;
        var cultureInfo = new CultureInfo("da-DK");
        var calendar = cultureInfo.Calendar;

        var week = calendar.GetWeekOfYear(datetime, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
        var weekString = datetime.Year + "-W" + week;

        var result = new JsonResult {Data = weekString};
        return result;
    }

      



This also solves the problem, but does nothing in the client.

0


source


Use substring

:



var foo = '"2015-W24"';
console.log(foo.substring(1, foo.length - 1)); // 2015-W24

      

0


source







All Articles