简体   繁体   English

路由问题还是参数问题?

[英]Routing issue or parameter issue?

I created 3 methods to test attribute routing. 我创建了3种方法来测试属性路由。
My 2 GET methods appear to be running, however I can't understand how to get my post method to work. 我的2个GET方法似乎正在运行,但是我不明白如何使我的post方法起作用。

Is my route wrong on the post method or am I incorrectly passing the wrong data to the method? 在post方法上,我的路线是否错误?或者我错误地将错误数据传递给了方法?

My controller: 我的控制器:

 [RoutePrefix("api/myTest")]
    public class JobController : ApiController
    {
        [Route("{id:int}")]
        [HttpGet]
        public string GetJob(int id)
        {
            return String.Format("Job-{0}", id.ToString());
        }

        [Route("GetJob2/{id:int}")]
        [HttpGet]
        public string GetJob2(int id)
        {
            return String.Format("New and improved Job-{0}", id.ToString());
        }

        [Route("NewJob/{data}")]
        [HttpPost]
        public HttpResponseMessage NewJob(HttpRequestMessage request)
        {
           // Read and process xml
        }
    }

In my JS I have: 在我的JS中,我有:

$.get('api/myTest/' + $("#jobID").val())  // Works
$.get('api/myTest/GetJob2/' + $("#jobID").val()) // Works
var data = "<root><name>Bob</name></root>";
$.post('api/myTest/NewJob/', data)  // Fails 404 Error

UPDATE: 更新:

    [Route("NewJob/{data}")]
    [HttpPost]
    public HttpResponseMessage NewJob(string request)

UPDATE 2: 更新2:

[Route("NewJob/{data}")]
        [HttpPost]
        public HttpResponseMessage NewJob(string data)  
        {

        }

var data = "<root><name>Bob</name></root>";
        $.post('api/myTest/NewJob/', data)
        .done(function (result) { alert("done"); })
        .fail(function (xhr, status, err) {
            alert(xhr.responseText);
        });

Straight from webapi Attribute Routing documentation and there is table of supported constraints too in the same link. 直接来自webapi 属性路由文档,并且在同一链接中也有受支持的约束表。

Route Constraints 路线约束

Route constraints let you restrict how the parameters in the route template are matched. 路由约束使您可以限制路由模板中参数的匹配方式。 The general syntax is "{parameter:constraint}". 通用语法为“ {parameter:constraint}”。 For example: 例如:

[Route("users/{id:int}"]
public User GetUserById(int id) { ... }

[Route("users/{name}"]
public User GetUserByName(string name) { ... }

Here, the first route will only be selected if the "id" segment of the URI is an integer. 在此,只有在URI的“ id”段为整数的情况下才选择第一个路由。 Otherwise, the second route will be chosen. 否则,将选择第二条路线。

In case of XML, you are not passing any data through url, so this is how your route is 如果是XML,则不会通过url传递任何数据,因此这就是路由的方式

[Route("NewJob")]
[HttpPost]
public HttpResponseMessage NewJob(HttpRequestMessage data)
{
               // Read and process xml
}

And specify xml in jQuery POST 并在jQuery POST中指定xml

var url = "";
var data = "";
$.ajax({
            contentType: "text/xml",
            dataType: "xml",
            type: "post",
            url: url,
            data: data
        });

Further, your previous call was 此外,您之前的通话是

$.post('api/myTest/NewJob/', data) it should be '+' instead of ','
$.post('api/myTest/NewJob/'+ data) 

lower one would give url invalid that is 400 instead of 404 较低的URL将使URL无效,而不是404

What happens is that your client is sending a request that should match an action with NewJob name, but the param type that your action is expecting is of type HttpRequestMessage , and the param in the request is of string type. 发生的情况是您的客户端发送的请求应与具有NewJob名称的操作匹配,但是您的操作期望的参数类型为HttpRequestMessage类型,而请求中的参数为string类型。

So change the action to this: 因此,将操作更改为此:

    [Route("NewJob/{data}")]
    [HttpPost]
    public HttpResponseMessage NewJob(string data)
    {
       // Read and process xml
    }

It should works now. 现在应该可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM