控制器:
public class DownloadListController : ApiController
{
private MovieService _movieService;
public DownloadListController()
{
_movieService = new MovieService();
}
public void Post([FromBody]string asd)
{
// Do something
}
public string Get()
{
return "test";
}
}
Global.asax中
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
WebApiConfig
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
当我运行应用程序并浏览到http://localhost:4229/api/DownloadList
时,我得到了404,而我在网络请求中看到的是GET
请求。
当我向URI发出POST
请求时,情况也是如此,结果是404.
我错过了什么?我需要做些什么来解决这个问题?
答案 0 :(得分:2)
Web API不使用显式操作。它们由HTTP动词暗示。因此,您的{action}
routeTemplate
routeTemplate: "api/{controller}/{id}",