c# - 如何在同一项目中使用 MVC Controller 和 WebAPI Controller

标签 c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

我试图在同一个项目中使用 MVC Controller 和 Web API Controller ,但我收到 Web API 的 404 错误。我在 VS 2015 中将项目作为 MVC 项目启动,但随后添加了 Web API Controller ,并使用默认代码给出了 404 错误。

可能的解决方案是什么?我在 Stack Overflow 上尝试了一些解决方案,但它们没有用。我试过的一个是这个问题的公认答案:All ASP.NET Web API controllers return 404

Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

WebApiConfig:

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 }
        );
    }
}

路由配置:

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 }
        );
    }
}

网络 API Controller :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using O365_APIs_Start_ASPNET_MVC.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using O365_APIs_Start_ASPNET_MVC.Helpers;
using System.Threading.Tasks;

namespace O365_APIs_Start_ASPNET_MVC.Controllers
{
    public class MAILAPIController : ApiController
    {
        private MailOperations _mailOperations = new MailOperations();
        //async Task<BackOfficeResponse<List<Country>>>

        // GET: api/MAILAPI
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/MAILAPI/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/MAILAPI
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/MAILAPI/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/MAILAPI/5
        public void Delete(int id)
        {
        }
    }
}

我在同一解决方案中恢复 NuGet 包时也遇到错误:

An error occurred while trying to restore packages: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

最佳答案

在为 MVC 注册路由之前,您需要为 Web API 注册路由,因此基本上您的 App_Start() 函数应该如下所示:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC 2nd
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

关于c# - 如何在同一项目中使用 MVC Controller 和 WebAPI Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201152/

相关文章:

c# - 如何在模型绑定(bind)中使用 Group By 查询

c# - ASP.NET Identity 在每次请求时重新生成 Identity

asp.net-mvc - 如何在复杂的 Web 应用程序中支持用户编辑的内容?

c# - 在 C# 中搜索名称列表的最快方法

c# - 在闲置的应用程序中,是什么导致我的 C#.NET 内存泄漏?

c# - "System.InvalidCastException: Specified cast is not valid"- 日期时间

c# - 为什么我的 Visual Studio 开始以这种方式运行? iisexpress 无法找到或打开 PDB 文件

c# - 通过网络服务生成 PDF 并将其作为附件通过电子邮件发送

c# - 使用 .NET(PowerShell 或 .NET)获取 BCD 条目

javascript - 访问代码隐藏中的 javascript 变量