使用下面的代码我点击了构造函数,但从未执行过操作。网址是 http://localhost:64704/api/mytest/mymethod。当我尝试更改这样的路由时出现同样的问题:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
[Route("api/[controller]")]
public class MyTestController : ApiController
{
readonly IMyFacade myFacade;
public MyTestController(IMyFacade myFacade) => this.myFacade = myFacade;
[HttpPost]
[Route("mymethod")]
public IHttpActionResult MyMethodName([FromBody]MyModel model)
{
//some code here
return Ok();
}
}
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 }
);
}
更新1
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
UnityConfig.RegisterComponents();
}
}
更新2
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
/* ...... */
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
更新3 { “Field1”:“1”, “Field2”:“一些关键”, “Field3”:“当然有些名字” }
答案 0 :(得分:2)
出现此问题的原因是您希望控制器的“路径”属性ontop与方法的“路径”属性合并。 这不是.net WebApi 2.0中的情况,(在.net core mvc中添加)
您应该使用控制器上的RoutePrefix属性来使控制器路由与方法路由合并
[RoutePrefix("api/[controller]")]
public class MyTestController : ApiController
{
readonly IMyFacade myFacade;
public MyTestController(IMyFacade myFacade) => this.myFacade = myFacade;
[HttpPost]
[Route("mymethod")]
public IHttpActionResult MyMethodName([FromBody]MyModel model)
{
//some code here
return Ok();
}
}
答案 1 :(得分:0)
<强> WebApiConfig 强>
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 }
);
}
}
<强> Global.asax中强>
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
<强>控制器强>
public class MyTestController : ApiController
{
[HttpPost]
[Route("mymethod")]
public IHttpActionResult MyMethodName([FromBody]MyModel model)
{
return Ok();
}
}
调试时
发送PostRequest
答案 2 :(得分:0)
在控制器中,您需要指定 RoutePrefix 属性,而不是路由,如下面的代码所示
and c.case_local_id like nvl('{CaseLocalId}','%')
再试一次。
答案 3 :(得分:-1)
您正在使用HttpPost动词,请确保在发送请求时使用HttpPost方法。