gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:07:12 25 4
gpt4 key购买 nike

我试图在同一个项目中使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com