区分GET方法中的ASP.NET Web API包含查询字符串 [英] Differentiating Get Method Containing Query String in ASP.NET WEB API

查看:164
本文介绍了区分GET方法中的ASP.NET Web API包含查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET网页API包含以下get方法:

I have a ASP.NET Web Api Containing following Get Methods:

[RoutePrefix("api/contact")]
public class ContactController : ApiController
{
    private ContactContext db = new ContactContext();

    public IEnumerable<Contact> Get()
    {
        return db.Contacts.AsEnumerable();
    }



    [Route("{id:int}")]
    public Contact Get(int id)
    {
        var result = db.Contacts.Find(id);
        if (result == null)
        {


            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return result;
    }

    public IEnumerable<Contact> GetContacts([FromUri]Filter filter)
    {
        return db.Contacts.Where(c => c.DepartmentId == filter.DepartmentId && c.City == filter.City).ToList();
    }
}

在功能公开的IEnumerable&LT;联系与GT; GetContactsfilter([FromUri]过滤器过滤器) DepartementId&安培;城市在查询字符串

In function public IEnumerable<Contact> GetContactsfilter([FromUri]Filter filter) DepartementId & City are in Query String like

localhost:12345/api/Contact?DepartmentId=2&city=Mumbai

我的问题是我怎么加属性的路由查询,因为
目前,它不能get方法区分并返回错误

My Question is how do I add Attribute Routing for Query,because Currently it cannot differentiate between the Get Methods and is returning error

{"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nSystem.Collections.Generic.IEnumerable`1[MyFirstWebApi.Models.Contact] Get() on type MyFirstWebApi.Controllers.ContactController\r\nSystem.Collections.Generic.IEnumerable`1[MyFirstWebApi.Models.Contact] GetContacts(MyFirstWebApi.Models.Filter) on type MyFirstWebApi.Controllers.ContactController","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"}

我尝试添加路由属性来获取包含方法 ID 但是,这并不奏效。

推荐答案

属性路由或常规路由不支持模板中的查询参数。相反,你可以修改你的控制器只是有如下两个动作:

Attribute routing or regular routing does not support query parameters in the templates. Instead you could modify your controller to just have two actions as below:

// Here if no DepartmentId or City are provided, then the 'filter' parameter would be null
[Route]
public IEnumerable<Contact> GetContacts([FromUri]Filter filter)

[Route("{id:int}")]
public Contact Get(int id)

这篇关于区分GET方法中的ASP.NET Web API包含查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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