得到“未找到动作".来自WebApi [英] Getting "No action was found" from WebApi

查看:292
本文介绍了得到“未找到动作".来自WebApi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WCF开发人员,是MVC的新手.试图将WCF API作为ApiControllers集成到MVC应用程序中(这是否值得努力仍是一个大问题).

I am a WCF dev, new to MVC. Trying to integrate my WCF API as ApiControllers into an MVC application (still big question if it's a worthy effort).

我有一个来自jQuery插件的请求:

I have this request from a jQuery plugin:

 POST http://localhost:18698/Api/Public/DoAction HTTP/1.1 
 ....
 Content-Type: application/json; charset=UTF-8 Accept:
 application/json, text/javascript, */*; q=0.01

 {"myParam":"test"}

我的控制器如下:

public class PublicController : ApiController
{
    [HttpPost]
    public string DoAction(string myParam)
    {
        return "Test";
    }
} 

而且,布线件看起来像这样:

And, the routing piece looks like this:

    config.Routes.MapHttpRoute(
        name: "PublicApi",
        routeTemplate: "api/{controller}/{action}"
    );

我收到此错误:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:18698/Api/Public/DoAction'.",
    "MessageDetail": "No action was found on the controller 'Public' that matches the request."
}

不接受任何JSON参数的方法可以正常工作,但是接受JSON参数的方法则无法工作.我必须能够将复杂的JSON传递给方法.在WCF中,WCF为我处理了从JSON到对象的转换.

Methods that don't accept any JSON parameters work fine, but the ones that accept JSON parameters don't work. I have to be able to pass complex JSON to the methods. In WCF, translation from JSON to objects was handled for me by WCF.

您知道为什么我会收到此错误吗?是否可以像在WCF中一样在MVC中无缝地传递/接收复杂的JSON?

Do you know why I am getting this error? Would I be able to pass/receive complex JSON as seamlessly in MVC as I did in WCF?

推荐答案

如果您检查发送的JSON {"myParam":"test"},那么您将看到Darin指出的内容,即您需要一个Model来包含您的属性,例如将{"myParam":"test"}运行到以下工具中: http://json2csharp.com/,您会得到:

If you examine the JSON you were sending {"myParam":"test"} then you will see what Darin points out i.e. you need a Model to contain your property e.g. run {"myParam":"test"} into the following tool: http://json2csharp.com/ and you get:

public class RootObject
{
    public string myParam { get; set; }
}

您以前使用简单字符串类型的方法签名在默认情况下将被WebApi视为UrlParameter(与public string DoAction([FromUri]string myFoo)相同).您可以证明这是您的代码,因为该网址应该可以正常工作:

Your previous method signature with the simple type of string would have been treated by WebApi as a UrlParameter by default (the same as public string DoAction([FromUri]string myFoo)). You can prove this as leaving you code as is this url should work:

http://localhost:50381/Api/Public/DoAction?myParam=something

使用JSON.NET的主体序列化程序将无法自行解析简单的.NET类型,因此您需要创建一个简单的Model来承载它.然后将使用from活页夹public string DoAction([FromBody]RootObject myFoo)

The body serialiser using JSON.NET won't be able to parse a simple .NET type on it's own and therefore you need to create a simple Model to host it on. This will then use the from body binder public string DoAction([FromBody]RootObject myFoo)

这篇关于得到“未找到动作".来自WebApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆