我在调用/ api / something / 67545217-0fed-4058-9cfc-f6363412f5a2时遇到了问题。它叫做全部。另一个Get的工作,但我认为这是因为他们在网址中使用了{action}。我正在使用[ActionName]装饰器。
这是我的路线:
public static void Register(HttpConfiguration config)
{
// enable CORS
config.EnableCors();
config.Services.Replace(typeof(IHttpControllerSelector), new ControllerSelector(config));
// enable OData Queries
// http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
config.EnableQuerySupport();
// http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Survey>("Survey");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{actionuniqueid}",
new
{
actionuniqueid =
@"^[A-Fa-f0-9]{32}$|({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$"
});
// Match All Regular Get Calls
config.Routes.MapHttpRoute("DefaultController", "api/{controller}");
config.Routes.MapHttpRoute(
"DefaultApiActionId",
"api/{controller}/{action}/{actionuniqueid}",
null,
new
{
action = @"^[a-zA-Z]+([\s][a-zA-Z]+)*$",
actionuniqueid =
@"^[A-Fa-f0-9]{32}$|({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$"
});
config.Routes.MapHttpRoute(
"DefaultApiTwoActionId",
"api/{controller}/{action}/{actionuniqueid}/{actionsecondaryid}",
null,
new
{
action = @"^[a-zA-Z]+([\s][a-zA-Z]+)*$",
actionuniqueid =
@"^[A-Fa-f0-9]{32}$|({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$",
actionsecondaryid =
@"^[A-Fa-f0-9]{32}$|({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$"
});
}
以下是我的所有内容...当我调用控制器时,只有ID ...没有调用正确的路径。是因为它具有与类型相同的设置吗?
public class TestController : ApiController
{
[Queryable]
[ResponseType(typeof(Test))]
public IQueryable<Test> Get(ODataQueryOptions opts)
{
IEnumerable<Test> test= (new Test()).PullList<Test>(LoadDepth.Self, "ByAll");
IQueryable results = test.AsQueryable();
return opts.ApplyTo(results) as IQueryable<Test>;
}
[HttpGet]
[Queryable]
[ActionName("ById")]
[ResponseType(typeof(Test))]
public IQueryable<Test> Get(ODataQueryOptions opts, Guid actionuniqueid)
{
IEnumerable<Test> test = (new Test()).PullList<Survey>(
LoadDepth.Everything,
"ById",
actionuniqueid);
IQueryable results = test.AsQueryable();
return opts.ApplyTo(results) as IQueryable<Test>;
}
[HttpGet]
[ActionName("ByType")]
[ResponseType(typeof(Test))]
public IQueryable<Test> GetByType(ODataQueryOptions opts, Guid actionuniqueid)
{
IEnumerable<Test> survey = (new Test()).PullList<Survey>(
LoadDepth.Everything,
"ByType",
actionuniqueid);
IQueryable results = test.AsQueryable();
return opts.ApplyTo(results) as IQueryable<Test>;
}
答案 0 :(得分:1)
"Microsoft.AspNet.WebApi.OData"
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<ViewTest>("TestOdata");
var model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute(routeName: "OData", routePrefix: "odata", model: model);
public class TestOdataController : OdataController
{
//GET odata/TestOdata
public IQueryable<ViewTest> Get()
{
try
{
return context.View_TestRepository.GetQueryable();
}
catch (Exception ex)
{
throw ex;
}
}
}
ViewTest:查看包含所需列的模型类 View_TestRepository:存储库类
您可以通过以下网址访问您的网络API:
"http://localhost:8080/odata/TestOdata"
答案 1 :(得分:1)
我只需要执行以下操作,添加了行动路线:
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{actionuniqueid}",
new
{
actionuniqueid =
@"^[A-Fa-f0-9]{32}$|({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$",
Action = "ById"
});
答案 2 :(得分:1)
不确定您是否仍使用v3或已升级到v4,但如果您仍使用Web API,则
config.MapHttpAttributeRoutes()
需要在Odata路由之前