[英]Unable to hit Web Api in .Net MVC
Hi I am just starting to mess with WebApi to setup a REST api for my angular app. 嗨,我刚开始与WebApi混在一起,为我的角度应用程序设置REST API。
I simply just added a "Web API 2 Controller with read/write actions" 我只是添加了“具有读/写操作的Web API 2控制器”
I then just try to test it by just typing the url in the browser but I get a 404. 然后,我只是尝试通过在浏览器中键入URL来对其进行测试,但得到了404。
Everything is pretty much out of the box. 几乎所有东西都开箱即用。
WebApiConfig.cs looks like WebApiConfig.cs看起来像
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig.cs is RouteConfig.cs是
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 }
);
}
}
Global.asax Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);
InitializeDatabase("Data Source=.;Initial Catalog=msproto;Integrated Security=True;MultipleActiveResultSets=True;");
}
Is there something I have to add to the web config? 我必须添加到Web配置中吗? If I add a normal controller "MVC 5 Controller with read/write actions" it will hit my breakpoints in the get requests.
如果添加普通控制器“具有读/写操作的MVC 5控制器”,它将在get请求中出现断点。
确保在Global.asax中的MVC路由之前注册了Web API路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.