Skip to content

Commit

Permalink
Test issue #64 and fix the "" response out issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Jan 8, 2021
1 parent 023f3b1 commit daea53c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Routing.Attributes;

namespace ODataRoutingSample.Controllers.v1
{
[ODataModel("v1")]
public class OrganizationsController : Controller
{
[HttpGet]
public IActionResult GetPrice([FromODataUri]string organizationId, [FromODataUri] string partId)
{
return Ok($"Caculated the price using {organizationId} and {partId}");
}
}
}
31 changes: 25 additions & 6 deletions sample/ODataRoutingSample/Extensions/ODataRouteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,42 @@ public static async Task HandleOData(HttpContext context)
action.Append(controllerActionDescriptor.MethodInfo.Name + "(");
action.Append(string.Join(",", controllerActionDescriptor.MethodInfo.GetParameters().Select(p => p.ParameterType.Name)));
action.Append(")");
string actionName = controllerActionDescriptor.MethodInfo.Name;

sb.Append("<tr>");
sb.Append($"<td>{controllerActionDescriptor.ControllerTypeInfo.FullName}</td>");
sb.Append($"<td>{action}</td>");

// http methods
string httpMethods = string.Join(",", metadata.HttpMethods);
sb.Append($"<td>{httpMethods}</td>");
sb.Append($"<td>{httpMethods.ToUpper()}</td>");

// OData routing templates
int index = 1;
sb.Append("<td>");
foreach (var template in metadata.Template.GetTemplates())
RouteEndpoint routeEndpoint = endpoint as RouteEndpoint;
if (routeEndpoint != null)
{
sb.Append($"{index++})").Append(" ~/").Append(template).Append("<br/>");
sb.Append("<td>~/").Append(routeEndpoint.RoutePattern.RawText).Append("</td></tr>");
}
else
{
int index = 1;
sb.Append("<td>");
foreach (var template in metadata.Template.GetTemplates())
{
sb.Append($"{index++})");
if (string.IsNullOrEmpty(metadata.Prefix))
{
sb.Append(" ~/");
}
else
{
sb.Append($" ~/{metadata.Prefix}/");
}

sb.Append(template).Append("<br/>");
}
sb.Append("</td></tr>");
}
sb.Append("</td></tr>");
}

string output = ODataRouteMappingHtmlTemplate.Replace("{CONTNET}", sb.ToString());
Expand Down
5 changes: 5 additions & 0 deletions sample/ODataRoutingSample/Models/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public class VipCustomer : Customer
{
public IList<string> Emails { get; set; }
}

public class Organization
{
public int OrganizationId { get; set; }
}
}
7 changes: 7 additions & 0 deletions sample/ODataRoutingSample/Models/EdmModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static IEdmModel GetEdmModel()
public static IEdmModel GetEdmModelV1()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Organization>("Organizations");
builder.EntitySet<Company>("Companies");
builder.EntitySet<Customer>("Customers");
builder.Singleton<Customer>("Me");
Expand All @@ -72,6 +73,12 @@ public static IEdmModel GetEdmModelV1()
boundAction.CollectionParameter<Address>("p4");
boundAction.CollectionParameter<Color?>("colors");

// bound function for organization
var productPrice = builder.EntityType<Organization>().Collection.
Function("GetPrice").Returns<string>();
productPrice.Parameter<string>("organizationId").Required();
productPrice.Parameter<string>("partId").Required();

return builder.GetEdmModel();
}

Expand Down

0 comments on commit daea53c

Please sign in to comment.