【问题标题】:More than one HttpPost Method in one Controller not working in Web Api一个控制器中的多个 HttpPost 方法在 Web Api 中不起作用
【发布时间】:2023-03-12 12:40:02
【问题描述】:

我有一个 Web Api 项目,首先我用一个 HttpPost 方法创建了一个控制器,然后它工作正常。但是当我添加了另一个 HttpPost 方法时,没有人在工作,但是当我删除任何人时,另一个人正在工作。我的代码是。

Web API 控制器:

 public class ForumPost
    {
        public int ProjectId { get; set; }
        public int CourseId { get; set; }
        public int SubjectId { get; set; }
        public int TopicId { get; set; }
        public int ContentId { get; set; }
        public string Heading { get; set; }
        public string Description { get; set; }
        public int UserId { get; set; }          
    }

    [HttpPost]
    [ActionName("AddForumPost")]
    public string AddForumPost([FromBody]ForumPost _ForumPost)
    {
        string strResult = "N";
        using (ICA.LMS.Service.Models.Entities db = new Entities())
        {

        }
        return strResult;
    }

    public class Comment
    {
        public int ForumId {get;set;}
        public string Response { get; set; }
        public int ParentId { get; set; }
        public int LevelId { get; set; }
        public string CreatedBy { get; set; }
    }

    [HttpPost]
    [ActionName("AddComment")]
    public string AddComment([FromBody]Comment cmt)
    {
        string strResult = "N";
        using (ICA.LMS.Service.Models.Entities db = new Entities())
        {

        }
        return strResult;
    }    

jQuery 调用:

var comment = { ForumId: fId, Response: res, ParentId: pId, LevelId: lId, CreatedBy: uId };

jQuery.support.cors = true;
$.ajax({        
    url: 'http://localhost:1900/api/ForumApi',
    type: 'Post',
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    data:comment,
    dataType: 'json',
    async: false,
    success: function (data) {
        var efId = $('#EncForumId').val();
        if (data == "Y")
            location.replace('../Forum/ForumDiscussion?id=' + efId);
        else
            myAlert('Unable to Post. Try again!');
    },
    error: function (e) {
        myAlert(JSON.stringify(e));
    }
});

错误:-

**{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:1900/api/ForumApi'."}**

【问题讨论】:

    标签: jquery asp.net asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    默认情况下web api路由是这样的

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    

    您会注意到路线中没有任何操作。

    Web API 根据 HTTP 动词和动作名称选择动作。您要么保留操作名称POST,要么使用注释HttpPost,要么保留以POST 开头的操作名称(PostComment

    要克服这个问题,如果您必须按动作名称进行路由,您需要添加这样的新路由

    routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    

    现在任何与api/{controller}/{action}/{id} 模式匹配的请求都将被此路由捕获,并将传递给相应的操作。

    你可以阅读更多关于这个here 此外,您现在必须更改您正在调用的网址,以便它们与新添加的路线相匹配 从http://localhost:1900/api/ForumApihttp://localhost:1900/api/ForumApi/AddForumPost 和其他网址一样

    【讨论】:

    • 我在哪里添加这个在 RouteConfig 或 WebApiConfig 中。
    • WebApiConfig。默认路由之前
    猜你喜欢
    • 2012-07-09
    • 2021-12-25
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多