500内部:找到与请求匹配的多个操作

时间:2017-04-04 12:50:13

标签: c# ajax asp.net-web-api routing

AJAX

 $.ajax({
                url: '../api/TechViews/SView',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify({ Filter: JSON.stringify(eth.Data), Type: $('.productType.active').data('name'), Name: $('#txtView').val() }),
                global: false,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                   alert('success');

                },
                error: function (xhrequest, ErrorText, thrownError) {
                   alert('Fail');
                }
            });

控制器

    [Route("SView")]
    [HttpPost]
    public string Post([FromBody]TechViewsModel value)
    {
        string result = string.Empty;
        return result;
    }

    [Route("DView")]
    [HttpPost]
    public string Post([FromBody]TechViewsModel value)
    {
        string result = string.Empty;
        return result;
    }

Route使用的命名空间是AttributeRouting.Web.Mvc。在AJAX来电时,我得到2个错误500 (Internal Server Error)

  1. 适用于TechViews
  2. 适用于SView
  3. ,回复为"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nSystem.String Post

    RouteConfig

     public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Login", action = "LoginPage", id = UrlParameter.Optional }
            );
        }
    

    我试过了,

    1. 删除HttpPost
    2. 重新订购HttpPostRoute
    3. 更改命名conntraints的名称
    4. 删除参数中的FromBody
    5. 更改方法名称和参数类型。
    6. 当我只使用一个没有Route的帖子时,相同的代码工作正常。

      我哪里错了?

1 个答案:

答案 0 :(得分:0)

在api路由配置中启用config.MapHttpAttributeRoutes()选项。

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

在global.asax.cs

GlobalConfiguration.Configure(WebApiConfig.Register);