Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebApi控制器中的方法与适当的AJAX调用不匹配_C#_Asp.net_Ajax_Asp.net Web Api - Fatal编程技术网

C# WebApi控制器中的方法与适当的AJAX调用不匹配

C# WebApi控制器中的方法与适当的AJAX调用不匹配,c#,asp.net,ajax,asp.net-web-api,C#,Asp.net,Ajax,Asp.net Web Api,我的控制器代码: public List<ContactDTO> GetAllContacts() { return repository.GetAllContacts().ToList(); } public ContactDTO GetContact(int contactId) { return repository.GetContact(contactId); } 我的ajax调用可以正常获取

我的控制器代码:

    public List<ContactDTO> GetAllContacts()
    {
        return repository.GetAllContacts().ToList();
    }

    public ContactDTO GetContact(int contactId)
    {
        return repository.GetContact(contactId);
    }
我的ajax调用可以正常获取所有联系人:

            $.ajax({
                method: "GET",
                url: "http://localhost:54185/contacts",
                dataType: "JSON",
                success: function (result) {
                    // result is ok
                }
            });
我已经编辑了RouteConfig和WebApiConfig文件,但我不确定我做的每件事都是对的

//RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Details",
                url: "contacts/details/{id}",
                defaults: new { controller = "Home", action = "Details", id = UrlParameter.Optional }
            );
        }
和WebApiConfig

public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(name: "contactDetails",
                routeTemplate: "{controller}/details/{id}");
        }

所以我不确定我错过了什么/做错了什么。在这种配置下,第一个ajax调用总是属于GetAllContacts方法,为了更好地理解,请尝试使用Atribute路由
在这种情况下,如果同时使用MVC和API,最好不要混淆路由方案。我不确定GetAllContacts在这种情况下如何工作,或者像前面的回答者所说的那样应用路由属性,或者重命名您的方法

我更喜欢将API保留在特定的分类中:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { 
                    controller = "DefaultApi", 
                    id = RouteParameter.Optional }
);
因此,在你的情况下,保持你的控制器和你的行动。也许可以用[HttpGet]来装饰它,以便更好地测量:

[HttpGet]
public List<ContactDTO> AllContacts()
{
    return repository.GetAllContacts().ToList();
}

尝试将第二个Web API路由移到第一个路由之上?
[HttpGet]
public List<ContactDTO> AllContacts()
{
    return repository.GetAllContacts().ToList();
}
$.ajax({
    method: "GET",
    url: "@(Url.RouteUrl("DefaultApi", new { httproute = false, controller = "Contacts", action = "AllContacts" }))",
    dataType: "JSON",
    success: function (result) {
        // result is ok
    }
});