C# WebApi Controller(States Controller目前有2个Get方法,1个返回List,第2个返回Single Item)

标签 c# asp.net asp.net-web-api odata asp.net-web-api-routing

我的 WebApiConfig:

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

            // Web API routes
            config.MapHttpAttributeRoutes();


            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Applicant>("Applicants");
            builder.EntitySet<Country>("Countries");
            builder.EntitySet<Program>("Programs");
            builder.EntitySet<Campus>("Campuses");
            builder.EntitySet<AcademicYear>("AcademicYears");
            builder.EntitySet<Citizenship>("Citizenships");
            builder.EntitySet<ProgramChoice>("ProgramChoices");
            builder.EntitySet<Application>("Applications");
            builder.EntitySet<ProvinceState>("States");

            config.MapODataServiceRoute(
              routeName: "ODataRoute",
              routePrefix: null,
              model: builder.GetEdmModel());




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

我的 Controller Get 方法返回一个项目列表:

   [EnableQuery]
        public IQueryable<ProvinceState> Get()
        {
            return db.ProvinceStates;
        }

我的 Controller Get 方法返回单个项目:

[EnableQuery]
public SingleResult<ProvinceState> Get([FromODataUri] string key)
{
    IQueryable<ProvinceState> result = db.ProvinceStates.Where(c => c.ProvinceStateCode == key);
    return SingleResult.Create(result);
}

问题如下:

  • 当我执行请求 LocalHost:###/States 时,Get 方法会毫无问题地正确返回状态列表。
  • 但是当我发出请求 LocalHost:###/States('AL') 以取回特定项目时,我收到以下错误。

HTTP 错误 404.0 - 未找到

您要查找的资源已被删除、更名或暂时不可用。

调试时,在向返回单个项目的 get 方法发出请求时,调试器不会为单个项目 GET 设置断点。但它在请求 List GET 方法时会发生。

任何建议或帮助将不胜感激,在此先感谢。

最佳答案

由于您有一个由 ProvinceStateCodeCountryCode 组成的复合键,您需要修改 Get 方法以接受这两个值。

[HttpGet]
[ODataRoute("States(ProvinceStateCode={stateCode},CountryCode={countryCode})")]
[EnableQuery]
public IHttpActionResult Get([FromODataUri] string stateCode, [FromODataUri] string countryCode)
{
    var result = db.ProvinceStates.FirstOrDefault(c => c.ProvinceStateCode == stateCode && c.CountryCode == countryCode);

    if (result == null)
    {
        return NotFound();
    }
    else
    {
        return Ok(result);
    }
}

然后按如下方式检索各个状态:

GET http://host/States(ProvinceStateCode='AL',CountryCode='US')

关于C# WebApi Controller(States Controller目前有2个Get方法,1个返回List,第2个返回Single Item),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36533226/

相关文章:

c# - 异步 GET 请求如何工作?

asp.net-web-api - 在 Dapper 中调用 QueryAsync 会抛出 InvalidOperationException

azure - 我该怎么做才能将 Katana/Owin 自托管 Web Api 添加为 Azure Web 角色上的服务

c# - 如何在 .NET 中检索已登录/已连接用户的列表?

c# - 在 .net 应用程序中嵌入流程图设计图面

ASP.NET DropDownList SelectedValue 属性未立即设置

c# - LINQ 连接表然后按主表分组

c# - 错误 : The type 'System.Data.OleDb.OleDbDataReader' has no constructors defined

c# - 取消选择 DataGridView 上的所有行

c# - Response.End() 与 HttpContext.Current.ApplicationInstance.CompleteRequest()