将WebAPI添加到现有的MVC项目中-获取404

达伦·温赖特(Darren Wainwright)

添加此内容时,有很多建议的问题/答案。我经历了一堆,似乎没有帮助。

我确定我缺少简单的东西,只是看不到它。

我有一个现有的ASP.Net MVC(V5)网站。这是一个页面。

我正在向其添加一个API控制器(WebAPI 2)来处理一些ajax帖子。

尝试发布到控制器时,我似乎无法摆脱404错误。

我也在使用属性路由。

这是我认为您需要查看的部分代码:

API控制器

[RoutePrefix("api/services")]
public class ServicesController : ApiController
{
    [HttpPost]
    [Route("contact")]
    public HttpResponseMessage SendContact(ContactModel model)
    {
        return Request.CreateResponse(HttpStatusCode.OK, true);
    }

}

WebApiConfig-在我尝试操作时注释掉了几个部分。

public class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {

        configuration.MapHttpAttributeRoutes();


        //configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
        //    new { id = RouteParameter.Optional });
    }
}

而我的global.cs-再次评论了我尝试过的事情

AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
//WebApiConfig.Register(GlobalConfiguration.Configuration);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);

尝试发布到api时,我收到:

POST http://localhost:8085/api/services/contact 404 (Not Found)

知道我缺少什么吗?

Aleksey L.

试穿

GlobalConfiguration.Configure(WebApiConfig.Register);

RouteConfig.RegisterRoutes(RouteTable.Routes);

所以应该是:

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将WebAPI添加到现有的aspnetcore MVC项目

如何将Vue.js添加到现有的ASP .NET Core 2.0 MVC项目中?

将单元测试项目添加到现有的Qt Creator项目中

使用dev将api添加到现有的rails项目中

如何将libgdx添加到现有的android studio项目中?

如何将监视模块添加到现有的Android Studio项目中?

如何将打字稿添加到现有的 vue3 项目中?

如何将pom.xml添加到现有的Eclipse项目中?

将Angular添加到现有的ASP.NET Web窗体项目中?

如何将scalatest依赖项添加到现有的scala / lift项目中?

将LibGDX添加到现有的Android Studio项目中

将drupal添加到现有的Angular项目中

将aspnetboilerplate.com添加到现有的角度项目中

如何将eslint添加到现有的vue-cli项目中?

将Web API添加到现有MVC Web应用程序后出现404错误

获取特定类型的文件并将其添加到现有的zip文件中

将版本添加到现有的WebAPI

将Composer添加到现有的Codeigniter项目

将 GeoDjango 添加到现有的 Django 项目

将Angle 6添加到现有的MVC项目

如何将Web API添加到现有的MVC Hottowel项目

使用 .Net 4 将 MVC 项目添加到现有的 Web 应用程序

如何将.aspx页添加到现有的MVC 4项目?

将Web Forms添加到现有的MVC项目

如何将Angular项目添加到现有的.NET Core Web API项目中?

将WebApi添加到现有的MVC项目-没有找到匹配名为“ x”的控制器的类型

如何将Web API添加到现有的ASP.NET MVC(5)Web应用程序项目中?

如何将Web API添加到现有的ASP.NET MVC 4 Web应用程序项目中?

将自托管的Asp.NET MVC 5添加到现有的winforms / console项目中