I'm trying to create a custom function in an OData v4 Web API solution. I need to return a collection of "Orders" based on unique logic that can't be handled natively by OData. I cannot seem to figure out how to create this custom function without destroying the entire OData service layer. When I decorate the Controller method with an ODataRoute attribute it all goes to hell. Any basic request produces the same error. Can someone please take a look at the code below and see if you notice something that I must be missing?
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapODataServiceRoute("odata", "odata", model: GetModel());
}
public static Microsoft.OData.Edm.IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Account>("Accounts");
builder.EntitySet<Email>("Emails");
builder.EntitySet<PhoneNumber>("PhoneNumbers");
builder.EntitySet<Account>("Accounts");
builder.EntitySet<Address>("Addresses");
builder.EntitySet<Order>("Orders");
builder.EntitySet<OrderDetail>("OrderDetails");
var orders = builder.EntityType<Order>();
var function = orders.Function("GetByExternalKey");
function.Parameter<long>("key");
function.ReturnsCollectionFromEntitySet<Order>("Orders");
return builder.GetEdmModel();
}
}
OrdersController.cs
public class OrdersController : ODataController
{
private SomeContext db = new SomeContext();
...Other Stuff...
[HttpGet]
[ODataRoute("GetByExternalKey(key={key})")]
public IHttpActionResult GetByExternalKey(long key)
{
return Ok(from o in db.Orders
where //SpecialCrazyStuff is done
select o);
}
}
}
When issuing ANY request against the OData layer I receive the following error response.
The path template 'GetByExternalKey(key={key})' on the action 'GetByExternalKey' in controller 'Orders' is not a valid OData path template. Resource not found for the segment 'GetByExternalKey'.
Per the model builder, the function GetByExternalKey is a bound function. According to the OData Protocol v4, a bound function is invoked through the namespace or alias qualified named, so you need to add more in the route attribute:
[HttpGet]
[ODataRoute("Orders({id})/Your.Namespace.GetByExternalKey(key={key})")]
public IHttpActionResult GetByExternalKey(long key)
{
return Ok(from o in db.Orders
where//SpecialCrazyStuff is done
select o);
}
If you don't know the namespace, just add below to the method GetModel():
builder.Namespace = typeof(Order).Namespace;
And replace "Your.Namespace" with the namespace of the type Order.
Here are 2 samples related to your question, just for your reference:
https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataFunctionSample/
https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataAttributeRoutingSample/
Related
I'm making an API in .NET Framework.
I have a controller called Autos, the API is meant to allow for the registration and retrieval of car models.
At first, I was able to use a GET to fetch an IEnumerable instance of List from Postman. I was getting the serialized version of a list I made with dummy data.
However, after I removed the dummy list, changed the method return to HttpResponseMessage and switched to a static dictionary I'd be using as a repository, the method stopped being executed upon calling it with Postman. I added logging messages and breakpoints and I can see that they just never execute.
My controller:
public class AutosController : ApiController
{
[HttpGet]
public HttpResponseMessage GetModels(HttpRequestMessage request)
{
//Theres a breakpoint here
System.Diagnostics.Debug.WriteLine(Repository.AutoRepository.Keys);
return request.CreateResponse<IEnumerable<Auto>>(HttpStatusCode.OK, Repository.AutoRepository.Values);
}
[HttpPost]
public HttpResponseMessage SetModel(HttpRequestMessage request, Auto serializedAuto)
{
if (serializedAuto.IsValid())
{
if (Repository.AddToRepository(serializedAuto))
return request.CreateResponse<Auto>(HttpStatusCode.Created, Repository.AutoRepository[serializedAuto.Id]);
else
return request.CreateResponse(HttpStatusCode.BadRequest);
} else
{
return request.CreateResponse(HttpStatusCode.BadRequest);
}
}
}
And my API Route
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I can successfully use SetModel and I can confirm that the sent model is also added to the repository. I am also able to return it to postman.
The postman address I'm using for the GET is
localhost:51020/api/v1/GetModels
the way i see it, your action could be
[Route("~/api/v1/GetModels")]
public IEnumerable<Auto> GetModels()
{
var values = Repository.AutoRepository.Values;
if (values==null) throw new HttpException(404, "Some description");
return values;
}
When using the following routes:
config.Routes.MapHttpRoute(
name: "new_device",
routeTemplate: "api/v1/devices",
defaults: new { controller = "Devices", action = "new_device" }
);
config.Routes.MapHttpRoute(
name: "devices_list",
routeTemplate: "api/v1/devices",
defaults: new { controller = "Devices", action = "devices_list", httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
The controller looks as follows:
public class DevicesController : ApiController
{
[HttpPost]
[ResponseType(typeof(IHttpActionResult))]
[Route("api/v1/devices")]
[ActionName("new_device")]
[ValidateModel]
public IHttpActionResult NewDevice([System.Web.Http.FromBody] Device device )
{
...
}
[HttpGet]
[ResponseType(typeof(IHttpActionResult))]
[Route("api/v1/devices")]
[ActionName("devices_list")]
[ValidateModel]
public List<Device> GetAllDevices()
{
...
}
My expectation would be that the router would find the correct route based on the HttpMethod used since even it's using the same URI it is using a different HttpMethod.
But instead it fails with the following:
"Message": "The requested resource does not support http method 'GET'."
My guess is because it fins a match with the URI and then checks if the method if the same.
Is there a way to achieve using the same URI with different Http Method which is by the way REST guidelines? Am I missing something?
Ok , I check your whole code. I think you are trying to achieve the calls in complicated way.
Following code is for the configuration :
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
and follwoing is your controller code :
public class DevicesController : ApiController
{
[HttpPost]
[ResponseType(typeof(IHttpActionResult))]
[ActionName("newDevice")]
public IHttpActionResult NewDevice([System.Web.Http.FromBody] Device device)
{
return null;
}
[HttpGet]
[ResponseType(typeof(IHttpActionResult))]
[ActionName("devices_list")]
public List<Device> GetAllDevices()
{
return null;
}
}
I removed ValidateModel. I think it's your custom attribute or somehow related with built in nuget package.
Anyways, execute the calls with Postman or any HTTP client tool. It should work , as it was working at my end with above mentioned code.
Example Calls:
https://localhost:44370/api/v1/devices/devices_list = > Get.
https://localhost:44370/api/v1/devices/newDevice => Post. Provide body as post call for the object.
I am using VS 2017 community. I have been building web api s for years. But something must have changed as I cannot get the simplest example to work.
I have a simple controller in the controller folder
public class TestApi : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
I have the necessary code in application start:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
But when I try and test the web api with a get like:
http://localhost:54014/api/testapi
I always get an xml message
Error
Message
No HTTP resource was found that matches the request URI
'http://localhost:54014/api/testapi'.
/Message
MessageDetail
No type was found that matches the controller named 'testapi'.
/MessageDetail
/Error
Here is the WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I am a couple of hours into head scratching on this. As I say I have built many MS web api implementations and this one has me baffled.
You should add Controller suffix to your class name.
public class TestApiController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
When the app starts, the asp.net mvc frameworks looks for classes (which are inherited from ApiController or Controller) with this suffix and register them when building the route table. When a request comes to your app, the framework again look into the route table and direct the request to the corresponding controller and action method.
Make this change, rebuild your project and try again.
In addition to the already provided answer (which is correct by the way) you can also consider using attribute routing over convention-based routing, where in this case it would not matter what the name of the controller is.
You already have it enabled Based on the WebApiConfig.cs and
config.MapHttpAttributeRoutes();
So now it is just a matter of putting the attributes where needed
[RoutePrefix("api/testapi")]
public class TestApi : ApiController {
[HttpGet]
[Route("")] //Matches GET api/testapi
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
}
Reference: Attribute Routing in ASP.NET Web API 2
I'm in the process of converting a web service to use OData.
I've created an ODataController implementation as below:
public class PersonController : ODataController
{
public PersonController()
{
}
public IHttpActionResult Get()
{
return Ok(new Person());
}
public IHttpActionResult Get([FromODataUri] int key)
{
return Ok(new Person());
}
protected override void Dispose(bool disposing)
{
}
}
And registered the model like so:
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>("Person");
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapODataServiceRoute(
routeName: "odata",
routePrefix: "odata",
model: builder.GetEdmModel());
The web app deploys without problems, and the first function works when I call:
http://localhost:9200/odata/Customer
->
{
"#odata.context":"http://localhost:9200/odata/$metadata#Person/$entity","Name":"John"
}
However, calling http://localhost:9200/odata/Customer(1) fails, the trace on the server log shows that the route is not found:
iisexpress.exe Information: 0 : Response, Status=404 (NotFound), Method=GET, Url=http://localhost:9200/odata/Person(1), Message='Content-type='application/xml; charset=utf-8', content-length=unknown'
I've tried different attribute permutations using ODataRoutePrefix, ODataRoute, EnableQuery on the method, and so far nothing I do seems to help. Tutorials I have seen say that this should work, so now I'm stuck wondering how I'm supposed to get this working. Does anyone have any ideas?
I managed to solve my own problem.
The problem was, the Key defined within the entity was of type String.
This meant that key extracted by FromODataUri had to also be of type String!
In this example, the problem could be fixed by changing key to String type, or by changing the Person key to int type.
I am using the default routing setup in WebApiConfig (MVC 4) but for some reason I am getting unexpected results.
If I call /api/devices/get/ it hits the Get function but the Id is "get" rather than 1. If I call /api/devices/get/1 I get a 404. I also want to be able to support multiple parameters i.e.
public Device[] Get(int? page, int? pageSize) // for multiple devices
The route
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
}
And my API:
public class DevicesController : ApiController
{
EClient client = new EClient();
// GET api/devices/5
public Device Get(string id)
{
return client.GetDeviceBySerial(id);
}
}
id in the controller parameter should be integer:
public Device Get(int id)
{
return client.GetDeviceBySerial(id);
}
if you need to pass in string, or other prams, just use quesry string:
public Device Get(int id, string pageSize)
{
return client.GetDeviceBySerial(id);
}
the above can be called as:
/api/devices/1
or
/api/devices/?id=1&pageSize=10
Note: you do not need to specify method name. Web API will judge that on the basis of HTTP Verb used. If its a GET request, it will use the Get method, if its a POST request, then it will use Post method ... and so on.
You can change the above behavior, but I guess you mentioned that you want to keep usign the default Routing ... so I am not covering that.