ASP MVC Web API端点返回404
我正在学习Angular 2并试图让它在ASP MVC设置上运行。我遵循这个非常优秀的指南:https://www.codeproject.com/Articles/1181888/Angular-in-ASP-NET-MVC-Web-API-PartASP MVC Web API端点返回404
我修改它非常轻微。原本应该处理通过API端点api/userapi
从数据库中获取的名称列表。我改变了它,以便它成为来自端点api/postapi
的帖子列表(如博客或其他内容)。该项目的角度部分目前工作。它的ASP.NET部分工作不正常。
查看原始项目的配置(我从该链接下载并在Visual Studio 2017中测试得很好)并将其与我的实验进行比较,对于我的生活,我无法找到可能错误配置的位置它。
这里有什么问题?这里是我的Web API部分代码:
Global.asax.cs
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace WebAPI
{
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);
}
}
}
RouteConfig.cs
using System.Web.Mvc;
using System.Web.Routing;
namespace WebAPI
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{*anything}", // I am suspicious of this line but changing it doesn't fix it
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
WebApiConfig.cs
using System.Net.Http.Headers;
using System.Web.Http;
namespace WebAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Output as JSON instead of XML
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("application/octet-stream"));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
PostsAPIController.cs
using WebAPI.Models;
using System.Data.Entity;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
namespace WebAPI.Controllers
{
[RoutePrefix("api/postapi")] //this did not help either
public class PostsAPIController : BaseAPIController
{
public HttpResponseMessage Get()
{
return ToJson(_db.TWebSitePostsSet.AsEnumerable());
}
public HttpResponseMessage Post([FromBody]TWebSitePosts value)
{
_db.TWebSitePostsSet.Add(value);
return ToJson(_db.SaveChanges());
}
public HttpResponseMessage Put(int id, [FromBody]TWebSitePosts value)
{
_db.Entry(value).State = EntityState.Modified;
return ToJson(_db.SaveChanges());
}
public HttpResponseMessage Delete(int id)
{
_db.TWebSitePostsSet.Remove(_db.TWebSitePostsSet.FirstOrDefault(x => x.PostId == id));
return ToJson(_db.SaveChanges());
}
}
}
改变此密码[RoutePrefix("api/postapi")]
到[Route("api/postapi")]
,看看是否有差别。
并查看此页面。 Attribute routing webapi-2
请您详细说明*为什么*这解决了OP的问题? –
这实际上有效。我第二个保罗的问题 - 为什么这个工作,以及为什么我的代码需要它,而教程代码与本教程中的代码几乎完全相同? –
你必须把这里的角代码API调用端点提供正确的答案.... –
你错过了HTTPS动词来识别REST调用> [HTTPGET] [HttpPost] ..... –
我想,但这是很多代码,并且它与为什么api url本身返回404如果直接进入它无关。 –