如何在同一项目中使用MVC控制器和WebAPI控制器 [英] How to Use MVC Controller and WebAPI Controller in same project

查看:385
本文介绍了如何在同一项目中使用MVC控制器和WebAPI控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在同一项目中使用MVC控制器和Web API控制器,但Web API出现404错误.我在VS 2015中将这个项目作为MVC项目开始,但是随后添加了Web API控制器,并使用默认代码显示404错误.

I am trying to use an MVC Controller and a Web API controller in the same project, but I get 404 errors for the Web API. I started the project as an MVC project in VS 2015, but then added the Web API controller, and with the default code it is giving a 404 error.

可能是什么解决方案?我已经尝试过一些关于Stack Overflow的解决方案,但是它们没有用.我尝试过的一个问题是该问题的可接受答案:所有ASP.NET Web API控制器返回404

What could be the possible solution? I have tried some solutions on Stack Overflow, but they didn't work. One I tried is the accepted answer from this question: 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 }
        );
    }
}

RouteConfig :

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

Web API控制器:

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软件包时也遇到错误:

I'm also getting an error restoring NuGet packages in the same solution:

尝试还原软件包时发生错误:指定的路径,文件名或两者都太长.完全限定的文件名必须少于260个字符,目录名称必须少于248个字符.

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.

推荐答案

您需要注册Web api的路由之前注册MVC的路由,因此基本上,您的App_Start()函数应类似于这个:

You need to register the routing for web api BEFORE registering the routing for MVC, so basically your App_Start()function should look like this:

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

这篇关于如何在同一项目中使用MVC控制器和WebAPI控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆