I have a controller defined as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MissingClinics.Controllers
{
[RoutePrefix("api/appointment")]
public class AppointmentController : ApiController
{
[HttpPost]
public IHttpActionResult GetMissingKeys([FromBody]String MRNList)
{
return Ok();
}
}
}
以及以下JavaScript调用该页面。
try {
$.ajax({
type: 'POST',
url: '/api/Appointment/GetMissingKeys',
data: $('#mrnList').val(),
dataType: 'text'
}).done(function () {
alert('done!');
}).fail(function (Status, errorThrown) {
alert('Error: ' + Status.status + ' - ' + Status.statusText);
}).always(function () {
alert('All done or not');
});
}
catch (e) {
alert(e);
}
WebApi.config是以下
namespace MissingClinics
{
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 }
);
}
}
}
RouteConfig.cs
namespace MissingClinics
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
现在因为我要传递相当数量的数据,我需要将它传递到正文中(而不是作为Url的一部分)。
但是在Call中它返回Error 405 - Method not allowed。但是从我可以看到jquery应该发出一个帖子请求并且控制器正在接受一个帖子 - 那么为什么不允许这个方法?
答案 0 :(得分:1)
使用默认路由模板,Web API使用HTTP方法选择操作。但是,您也可以创建一个路径,其中操作名称包含在URI中:
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
答案 1 :(得分:1)
因为我使用了jquery但
已经有一段时间了不应该
data : {mrnList: $('#mrnList').val()}, //Or MrnList depending on your jsonformatting options
是这个
'json'
我认为dataType应该是[Route("GetMissingKeys")]
。
您是否尝试在方法中添加using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
using (var context = new TestDbContext())
{
var teams = context.Teams.ToList();
var ideas = context.Ideas.ToList();
var portfolios = context.Portfolios.ToList();
}
}
}
public class TestDbContext : DbContext
{
public DbSet<Team> Teams { get; set; }
public DbSet<Idea> Ideas { get; set; }
public DbSet<Portfolio> Portfolios { get; set; }
}
public class Portfolio
{
[Key]
[Column(Order = 1)]
public int Id { get; set; }
public string Name { get; set; }
[Key, ForeignKey("Idea")]
[Column(Order = 2)]
public int IdeaId { get; set; }
public virtual Idea Idea { get; set; }
[Key, ForeignKey("Team")]
[Column(Order = 3)]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
}
public class Idea
{
public int Id { get; set; }
public string Name { get; set; }
[Key, ForeignKey("Team")]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
}
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Portfolio> Portfolios { get; set; }
[Key, ForeignKey("Idea")]
public int IdeaId { get; set; }
public virtual Idea Idea { get; set; }
}
}
?
答案 2 :(得分:0)
您是否已将默认路线从api/{action}
更改为api/{controller}/{action}/{id}
?
1.更改web api默认路由
WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
有了这个,你不需要改变你的html文件。
2.更改您的行动名称
public IHttpActionResult Post([FromBody]String MRNList)
并像这样调用javascript:
$.ajax("/api/Appointment",
{type:"POST",
data:$("").val()
...
}