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

普拉迪普

我有一个使用Visual Studio模板创建的Hottowel项目。我想在该项目中添加Web API功能。我已经创建了一个Web Api控制器到控制器文件夹,并尝试访问,"http://localhost:53397/api/Values"但是出现The resource cannot be found错误提示错误。

我的控制器代码如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MvcApplication8.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

我在APP_start文件夹中有一个名为BreezeWebApiConfig.cs的CS文件,该文件包含映射路由的逻辑,如下所示。

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
          name: "BreezeApi",
          routeTemplate: "api/{controller}/{action}"
      );

如果我缺少Web APi的任何配置设置,请告诉我。

Ouedraogo先生

尝试像下面这样装饰ApiController:

[BreezeController]
public class NorthwindIBModelController : System.Web.Http.ApiController {

    readonly EFContextProvider<NorthwindIBContext> ContextProvider =
         new EFContextProvider<NorthwindIBContext>();

    [HttpGet]
    public String Metadata() {
      return ContextProvider.Metadata();
    }

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle) {
      return ContextProvider.SaveChanges(saveBundle);
    }

    [HttpGet]
    public IQueryable<Customer> Customers() {
      return ContextProvider.Context.Customers;
    }

欲了解更多信息,请在这里查看文档

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

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

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

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

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

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

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

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

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

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

如何将Firebase Cloud功能样板添加到现有的Firebase Web项目中?

将Composer添加到现有的Codeigniter项目

将 GeoDjango 添加到现有的 Django 项目

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

将 ASPNET API 添加到现有的 Worker Service 项目

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

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

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

如何将现有的Eclipse Java项目添加到git

如何将现有的Python文件导入/添加到PyCharm项目?

如何将现有的非git项目添加到bitbucket

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

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

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

如何将jQuery Mobile添加到现有的Worklight 6.2项目

如何将dbmate添加到现有项目?

如何将现有项目添加到TFS

如何将Web API控制器添加到现有的ASP.NET Core MVC?

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