小编Mag*_*ing的帖子

没有MediaTypeFormatter可用于从媒体类型为"text/plain"的内容中读取"String"类型的对象

情况就是这样:

他们是Servoy中的外部Web 服务,我想在ASP.NET MVC应用程序中使用此服务.

使用此代码,我尝试从服务中获取数据:

HttpResponseMessage resp = client.GetAsync("http://localhost:8080/servoy-service/iTechWebService/axws/shop/_authenticate/mp/112818142456/82cf1988197027955a679467c309274c4b").Result;
resp.EnsureSuccessStatusCode();

var foo = resp.Content.ReadAsAsync<string>().Result;
Run Code Online (Sandbox Code Playgroud)

但是当我运行应用程序时,我得到了下一个错误:

没有MediaTypeFormatter可用于从媒体类型为"text/plain"的内容中读取"String"类型的对象.

如果我打开Fiddler并运行相同的url,我会看到正确的数据,但内容类型是text/plain.但是我在Fiddler中也看到了我想要的JSON ......

是否有可能在客户端解决这个问题,还是Servoy Web服务?

更新:
使用HttpWebRequest而不是HttpResponseMessage并使用StreamReader读取响应...

.net c# asp.net-mvc json httpclient

57
推荐指数
3
解决办法
8万
查看次数

JSON中的ASP.NET Web API日期格式未成功序列化

所有,

我们正在使用ASP.NET Web API,我们有一个基于REST的服务,其中包含有效负载的JSON.如果我将以下日期作为字符串传递,例如

sampleObj: {
...
myDate: "31/12/2011 00:00:00",
...
}
Run Code Online (Sandbox Code Playgroud)

作为JSON有效内容中的属性值,date属性被反序列化为DateTime.MinValue.字符串格式有效吗?

我们知道格式"2012-10-17 07:45:00"成功序列化,但我们不能保证收到的所有日期都是这种格式.什么是有效的选择?

c# serialization json.net asp.net-web-api

18
推荐指数
1
解决办法
3万
查看次数

405消息,Web Api不允许的方法

我在API控制器上有以下内容:

public void UpdateClient(Client client)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Entry(client).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
        catch
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
    }
Run Code Online (Sandbox Code Playgroud)

以及页面上的以下内容:

$.ajax({
            url: "api/client/UpdateClient",
            type: "PUT",
            contentType: 'json',
            data: ko.toJSON(model.selectedClient()),
            success: function (result) {
                getClients();
                $("#loader").hide();
            },
            failure: function (result) {
                alert(result.d);
                $("#loader").hide();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("An error occurred, please try again.");
                $("#loader").hide();
            }
        });
Run Code Online (Sandbox Code Playgroud)

但这会给错误405方法不允许,任何人都可以看到我可能出错的地方?作为参考,api的url是可以的,因为我也使用相同的api控制器来执行其他功能.

selectedClient()也是通过WebApi接收的Client对象,因此应该与PUT完全匹配.

asp.net-mvc asp.net-web-api

17
推荐指数
3
解决办法
3万
查看次数

Web API中的多态性:单端点可能吗?

我意识到Web API是以REST为重点的,但我仍然希望配置一个可以处理命令/响应方案的控制器方法.到目前为止,我还没有成功......有没有办法让单个API端点识别以下类结构?

[Serializable]
public abstract class Command{
    public int CommandId{get; set;}
}
[Serializable]
public class RegisterNewPersonCommand:Command{
   public string Name{get; set;}
}
//etc... various Command subclasses. Similar thing for Responses.

//now have a single endpoint to Handle Commands
public class CommandsController : ApiController{
   public Response HandleCommand(Command command){
      //handle Command based on which subclass it is
      //return appropriate Response subclass
   }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,似乎序列化系统似乎无法处理这种情况,但我希望那里有人找到了办法:)

asp.net-web-api

13
推荐指数
1
解决办法
4071
查看次数

删除<ArrayOf.来自MVC Web Api的回复

我从标准的MVC 4 WebApi项目获得以下响应;

<ArrayOfProduct xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Product>
<Id>1</Id>
<Name>Tomato Soup</Name>
<Category>Groceries</Category>
<Price>1</Price>
</Product>
</ArrayOfProduct>
Run Code Online (Sandbox Code Playgroud)

我试图让它返回

<Products>
<Product>
<Id>1</Id>
<Name>Tomato Soup</Name>
<Category>Groceries</Category>
<Price>1</Price>
</Product>
</Products>
Run Code Online (Sandbox Code Playgroud)

我发现许多参考各种方法可以解决这个问题,没有工作;

更改默认序列化程序不起作用.

创建客户序列化程序Product不起作用.

创建List<Product>使用适当的XmlRoot和XmlElement属性公开的新类不起作用.

添加Datacontract属性不起作用.

添加CollectionDatacontract属性不起作用.

这对其他人来说似乎很简单,除了我!

xml asp.net-mvc asp.net-mvc-4 asp.net-web-api

12
推荐指数
1
解决办法
5217
查看次数

包含循环,如果禁用参考跟踪,则无法序列化,json.net和webapi

我收到错误:

 Object graph for type 'System.Collections.Generic.List`1[[Proj.Model.Prom, Proj.Model, 
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be 
 serialized if reference tracking is disabled.
Run Code Online (Sandbox Code Playgroud)

读到这一点,似乎是序列化器,但Json.Net声称是解决方案,我读过WebApi,而Framework 4.5默认使用它.所以它是默认出现的吗?如果是这样,为什么我仍然会收到该错误?

谢谢!吉列尔莫.

编辑:添加代码

using System;
using System.Collections.Generic;
using System.Data.Spatial;

namespace Proj.Model
{
    public class Prom
    {
        public Prom()
        {
            this.Stores = new List<Store>();
            this.Branches = new List<Branch>();
            this.Products = new List<Product>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public DbGeography Location { get; set; }
        public string Latitude { get; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first asp.net-web-api entity-framework-5

12
推荐指数
1
解决办法
2万
查看次数

上传文件MVC 4 Web API .NET 4

我正在使用Visual Studio 2012 Express附带的MVC版本.(Microsoft.AspNet.Mvc.4.0.20710.0)

我假设这是RTM版本.

我在网上找到了很多使用这段代码的例子:

    public Task<HttpResponseMessage> PostFormData()
    {
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        // Read the form data and return an async task.
        var task = Request.Content.ReadAsMultipartAsync(provider).
            ContinueWith<HttpResponseMessage>(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                }

                // This illustrates how to get the file names.
                foreach (MultipartFileData file in provider.FileData)
                {
                    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                    Trace.WriteLine("Server file path: " + …
Run Code Online (Sandbox Code Playgroud)

.net-4.0 asp.net-mvc-4 asp.net-web-api

8
推荐指数
1
解决办法
1万
查看次数

在Web APi中的每个操作之后触发某些方法

我正在编写ASP.NET WEB API.

执行Action后,我想调用一个方法.

例如:

 public string Action1(object a)
 {
     // ...
     // call method1();
     return "sample1";
 }

 public string Action2(object b)
 {
     // ...
     // call method1();
     return "sample2";
 }
Run Code Online (Sandbox Code Playgroud)

有没有办法method1()在不提及每一个动作的情况下调用每个动作?

c#-4.0 asp.net-web-api

3
推荐指数
1
解决办法
1287
查看次数

"#"列中的红色圆圈表示什么?

"#"列中的红色圆圈表示什么?
截图

当我调试一个只在IE上进行重新编程的问题时,我遇到了这个问题.(仅供参考,问题是响应中的JSON无法绑定到我在IE上的淘汰模型......但它在Chrome上工作正常......).我已经尝试重播该请求(重播>重发请求),我看到第二个请求返回一个'{json}'图标.

我试图理解小提琴手在第一次请求时告诉我的内容.红圈交叉意味着什么?

fiddler

1
推荐指数
1
解决办法
4611
查看次数