我的自托管(OWIN)WebAPI 2 / OData 4应用程序有一个奇怪的情况:4个控制器中有3个工作正常,但是一个控制器给了我404。 这是我的startup.cs文件,我在其中构建了我的edm:
using System;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Owin;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using Owin;
using Nancy;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.Http.Cors;
using CommonDataService.Models;
namespace SelfHostedWebApiDataService
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Configure Web API for self-host.
var config = new HttpConfiguration();
var corsAttr = new EnableCorsAttribute("*", "*", "*", null);
config.EnableCors(corsAttr);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ChangeMeasure>("ChangeMeasure");
builder.EntitySet<Account>("Account");
builder.EntitySet<AccountAlia>("AccountAlias");
builder.EntitySet<AccountTool>("AccountTools");
builder.EntitySet<Role>("Role");
builder.EntitySet<Person>("Person");
builder.EntitySet<AccountRolePerson>("AccountRolePerson");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
// Adding to the pipeline with our own middleware
app.Use(async (context, next) =>
{
// Add Header
context.Response.Headers["Product"] = "Common Data Service";
// Call next middleware
await next.Invoke();
});
// Custom Middleare
app.Use(typeof(SelfHostedWebApiDataService.CustomMiddleware));
// Web Api
app.UseWebApi(config);
// File Server
var options = new FileServerOptions
{
EnableDirectoryBrowsing = true,
EnableDefaultFiles = true,
DefaultFilesOptions = { DefaultFileNames = {"index.html"}},
FileSystem = new PhysicalFileSystem("Assets"),
StaticFileOptions = { ContentTypeProvider = new SelfHostedWebApiDataService.CustomContentTypeProvider() }
};
app.UseFileServer(options);
// Nancy
app.UseNancy();
}
}
}
这是我的实体:
using System;
using System.Collections.Generic;
namespace CommonDataService.Models
{
public partial class AccountRolePerson
{
public AccountRolePerson()
{
this.Accounts = new List<Account>();
this.People = new List<Person>();
this.Roles = new List<Role>();
}
public int AccountRolePersonID { get; set; }
public string AccountID { get; set; }
public int PersonID { get; set; }
public int RoleID { get; set; }
public virtual ICollection<Account> Accounts { get; set; }
public virtual ICollection<Person> People { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
}
以下是此实体的WebAPI / OData控制器:
using CommonDataService.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.OData;
namespace CommonDataService.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
class AccountRolePersonController : ODataController
{
MALContext db = new MALContext();
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<AccountRolePerson> Get()
{
return db.AccountRolePersons;
}
[EnableQuery]
public SingleResult<AccountRolePerson> Get([FromODataUri] string key)
{
IQueryable<AccountRolePerson> result = db.AccountRolePersons.Where(p => p.AccountID == key);
return SingleResult.Create(result);
}
}
}
当我在fiddler:http://windows-10:8888/AccountRolePerson
中输入我的api url时,我得到一个404,而与其他实体相关联的其他3个控制器工作得很好。
请有人帮助我找到这种奇怪情况的原因吗?
答案 0 :(得分:4)
您的控制器不是public
类。