Ich habe eine seltsame Situation in meiner selbst gehosteten (OWIN) WebAPI 2/OData 4 App: 3 von 4 Controllern funktionieren gut, aber einer Controller gibt mir 404. Hier ist meine startup.cs Datei, in der ich meine edm bauen:selbst gehostete WebAPI 2/OData 4 App - alle Controller arbeiten, aber eine
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();
}
}
}
Hier ist meine Einheit in Frage:
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; }
}
}
Hier ist die WebAPI/OData-Controller für diese Einheit:
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);
}
}
}
Wenn ich meine API in fiddler eingeben: http://windows-10:8888/AccountRolePerson
, bekomme ich eine 404, während andere 3 Controller, die mit anderen Entitäten verbunden sind, gut funktionieren.
Könnte mir bitte jemand helfen, den Grund für diese seltsame Situation zu finden?