如何在同一个项目中使用MVC Controller和WebAPI Controller

时间:2016-08-29 07:54:12

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

我正在尝试在同一个项目中使用MVC Controller和WebAPI控制器,但是我为webapi得到了404错误。我在vs 2015中作为MVC项目启动了项目,但后来添加了webapi控件,默认代码为404错误

什么是可能的解决方案。我已经在Stackoverflow上尝试了一些解决方案,但他们没有工作,其中一个是在链接下面(那里的接受的答案) 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);

        }

WEBAPI.CONFIG文件

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

        }
    }

WebAPI控制器代码

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恢复错误 Error Nuget failed to restore PNG

4 个答案:

答案 0 :(得分:21)

您需要为web api 注册路由选择注册MVC的路由,因此基本上您的df <- structure(list(v1 = c("0-0098-45.3A-22", "0-0104-44.0A-23", "0-0983-29.1-22", "0-1757-42.5A-22", "0-4968-37.3A2-23", "000000-44.0a-23", "000000-45.3A-42" )), .Names = "v1", class = "data.frame", row.names = c(NA, -7L)) 函数应如下所示:

App_Start()

答案 1 :(得分:0)

只需在全局asax页面的Application_Start()方法中添加一行。

enter image description here

答案 2 :(得分:0)

您有两条路线以相同的方式映射。删除delete(id)或get(id)。或者,您可以在路线映射中添加操作。

答案 3 :(得分:0)

您可以像这样将两个配置写入同一文件

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

    }
 public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    }
   }

在Global.aspx文件中编辑这样的代码-

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