简体   繁体   English

OData路由返回404 Not Found

[英]OData routes return 404 Not Found

I've started including OData in my WebAPi2 project (currently hosted in IIS8 Express on my dev machine). 我已经开始在我的WebAPi2项目(当前托管在我的开发机上的IIS8 Express中)中包括OData。 My OData config class looks like this: 我的OData配置类如下所示:

public class ODataConfig
{
    private readonly ODataConventionModelBuilder modelBuilder;

    public ODataConfig()
    {
        modelBuilder = new ODataConventionModelBuilder();

        modelBuilder.EntitySet<Category>("Category");
    }

    public IEdmModel GetEdmModel()
    {
        return modelBuilder.GetEdmModel();
    }
}

Then I added the following in my WebApiConfig class: 然后,我在WebApiConfig类中添加了以下内容:

ODataConfig odataConfig = new ODataConfig();

config.MapODataServiceRoute(
    routeName: "ODataRoute",
    routePrefix: "MyServer/OData",
    model: odataConfig.GetEdmModel(),
    defaultHandler: sessionHandler
);

And started with a basic controller and just one action, like this: 并从一个基本的控制器和一个动作开始,就像这样:

public class CategoryController : ODataController
{
    [HttpGet]
    public IHttpActionResult Get([FromODataUri] int key)
    {
        var entity = categoryService.Get(key);
        if (entity == null)
            return NotFound();

        return Ok(entity);
    }
}

Then, in my HttpClient, the request url looks like this: MyServer/OData/Category(10) 然后,在我的HttpClient中,请求网址如下所示:MyServer / OData / Category(10)

However, I'm getting the following error: 但是,出现以下错误:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/MyServer/OData/Category(10)'.","MessageDetail":"No type was found that matches the controller named 'OData'."}

What am I missing here? 我在这里想念什么?

EDIT 编辑

If I set the routePrefix to null or 'odata' and change my request url accordingly, the request works fine. 如果我将routePrefix设置为null或'odata'并相应地更改我的请求网址,则该请求可以正常运行。 So this means that I can't have a route prefix like 'myServer/odata'. 因此,这意味着我不能具有“ myServer / odata”之类的路由前缀。

Is this OData standard naming convention? 这是OData标准命名约定吗? And if yes, can it be overridden? 如果是,可以覆盖它吗?

This is probably too late, but for anyone else that ends up here... 这可能为时已晚,但对于到此为止的其他任何人...

I don't think the problem is odata. 我认为问题不是odata。 Perhaps you're running foul of the default routing as the message "No type was found that matches the controller named 'OData'" suggests that http://localhost/MyServer/OData/Category(10) is being routed using 可能您正在使用默认路由,因为消息“未找到与名为'OData'的控制器匹配的类型”消息表明正在使用以下命令路由http://localhost/MyServer/OData/Category(10)

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters

so it's looking for a controller called ODataController with an action "Category". 因此,它正在寻找具有操作“类别”的名为ODataController的控制器。 You need to define "localhost/MyServer" as the root from which the routing is applied. 您需要定义“ localhost / MyServer”作为应用路由的根目录。 Unfortunately I can't suggest how you might do that but hopefully this points you in the right direction. 不幸的是,我无法建议您怎么做,但希望这会为您指明正确的方向。

I've been using the same WebApiConfig.Register() method that is included by default in the Web API project and passing using the following: 我一直在使用相同的WebApiConfig.Register()方法,该方法默认包含在Web API项目中,并使用以下方法进行传递:

var builder = new ODataConventionModelBuilder();

// OData entity sets..
builder.EntitySet<Seat>("Seats");
builder.EntitySet<Table>("Tables");

// Configure the Route
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());

The first parameter is a friendly name, the second is the one you're after! 第一个参数是一个友好的名称,第二个参数是您要使用的名称! You can change this to whatever you want. 您可以将其更改为任何您想要的。

UPDATE: If you're using OData V4, the routing is initialised like this: 更新:如果您使用的是OData V4,则路由初始化如下:

config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());

If you're using V4, method based routing via the use of attributes is now available (think Nancy style) 如果您使用的是V4,则现在可以使用通过属性进行基于方法的路由(想想Nancy风格)

You can use this in either an OWIN startup class or the Global.asax. 您可以在OWIN启动类或Global.asax中使用它。 Either way works fine for me. 无论哪种方式对我来说都很好。

Refer: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/creating-an-odata-endpoint 请参阅: http : //www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/creating-an-odata-endpoint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM