C# Web Api属性路由赢得';不匹配POST请求
我在控制器上的所有操作上都有属性路由(尽管定义了默认的常规路由)。我(我想)尝试过使用[HttpPost]或不使用[FromBody]的各种组合 如果我尝试使用仅定义了控制器名称的属性C# Web Api属性路由赢得';不匹配POST请求,c#,asp.net-mvc-5,asp.net-web-api2,asp.net-web-api-routing,C#,Asp.net Mvc 5,Asp.net Web Api2,Asp.net Web Api Routing,我在控制器上的所有操作上都有属性路由(尽管定义了默认的常规路由)。我(我想)尝试过使用[HttpPost]或不使用[FromBody]的各种组合 如果我尝试使用仅定义了控制器名称的属性 [HttpPost] [Route("Relationships")] private async Task<IHttpActionResult> PostRelationship([FromBody]Relationship relationship) Request URL:http://loc
[HttpPost]
[Route("Relationships")]
private async Task<IHttpActionResult> PostRelationship([FromBody]Relationship relationship)
Request URL:http://localhost:51599/api/Relationships
Request method:POST
我使用的AJAX称之为:
async function postEdge() { // persists a new edge to data store from form
var ItemLeftId = $('#ItemLeftId').val();
var ItemRightId = $('#ItemRightId').val();
var RelType = $('#ddlRelType').val();
var Description = $('#Description').val();
$.ajax({
method: "POST",
contentType: "application/json; charset=utf-8",
url: '/api/Relationships',
data: {
Id: 0, // TESTING
ItemLeftId: ItemLeftId,
ItemRightId: ItemRightId,
RelType: RelType,
Description: Description,
},
我想出来了——如果你看上面,GetRelationship和PostRelationship方法都设置为Private。这是我从另一个基类继承关系并使用不同的公共方法进行区分时遗留下来的。这是一个愚蠢的错误,但是如果症状看起来不是那么奇怪/不相关,那么就更容易发现。我发现了——如果你看上面,GetRelationship和PostRelationship方法都设置为Private。这是我从另一个基类继承关系并使用不同的公共方法进行区分时遗留下来的。这是一个愚蠢的错误,但如果症状看起来不那么奇怪/不相关,那么就更容易被发现
[HttpPost]
[Route("Relationships/PostRelationship")]
private async Task<IHttpActionResult> PostRelationship([FromBody]Relationship relationship)
http://localhost:51599/api/Relationships/PostRelationship
Request method:POST
routeData {System.Web.Routing.RouteData} System.Web.Routing.RouteData
+ DataTokens {System.Web.Routing.RouteValueDictionary} System.Web.Routing.RouteValueDictionary
+ Route {System.Web.Http.WebHost.Routing.HttpWebRoute} System.Web.Routing.RouteBase {System.Web.Http.WebHost.Routing.HttpWebRoute}
+ RouteHandler {System.Web.Http.WebHost.HttpControllerRouteHandler} System.Web.Routing.IRouteHandler {System.Web.Http.WebHost.HttpControllerRouteHandler}
- Values {System.Web.Routing.RouteValueDictionary} System.Web.Routing.RouteValueDictionary
Count 2 int
- Keys Count = 2 System.Collections.Generic.Dictionary<string, object>.KeyCollection
[0] "controller" string
[1] "id" string
- Values Count = 2 System.Collections.Generic.Dictionary<string, object>.ValueCollection
[0] "Relationships" object {string}
[1] "PostRelationship" object {string}
[RoutePrefix("api")]
public class RelationshipsController : ApiController
{
private CMDBContext db = new CMDBContext();
[Route("Relationships")]
public IQueryable<Relationship> GetRelationships()
{
return db.Relationships;
}
[Route("Relationships/{id:int}")]
private async Task<Relationship> GetRelationship(int id)
{
Relationship relationship = await db.Relationships.FindAsync(id);
if (relationship == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return relationship;
}
[HttpPost]
[Route("Relationships/PostRelationship")]
private async Task<IHttpActionResult> PostRelationship([FromBody]Relationship relationship)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Relationships.Add(relationship);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = relationship.Id }, relationship);
}
[HttpGet]
[Route("Relationships/RelTypes")]
public Dictionary<string, string> RelTypes() // returns a list of available Relationship types
{
var RelTypesDict = new Dictionary<string, string>();
foreach (var type in Relationship.RelTypes)
{
RelTypesDict.Add(type, Regex.Replace(type, @"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))", " $1")); // expands camel case with spaces for display
}
return RelTypesDict;
}
private bool RelationshipExists(int id)
{
return db.Relationships.Count(e => e.Id == id) > 0;
}
}
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
async function postEdge() { // persists a new edge to data store from form
var ItemLeftId = $('#ItemLeftId').val();
var ItemRightId = $('#ItemRightId').val();
var RelType = $('#ddlRelType').val();
var Description = $('#Description').val();
$.ajax({
method: "POST",
contentType: "application/json; charset=utf-8",
url: '/api/Relationships',
data: {
Id: 0, // TESTING
ItemLeftId: ItemLeftId,
ItemRightId: ItemRightId,
RelType: RelType,
Description: Description,
},