Ich versuche, MVC Controller und WebAPI Controller in demselben Projekt zu verwenden, aber ich habe 404 Fehler für Webapi erhalten. Ich habe das Projekt als MVC-Projekt in 2015 gestartet, aber dann webapi controlle hinzugefügt und mit Standardcode gibt es 404-Fehler
was könnte eine mögliche Lösung sein. Ich habe eine Lösung für Stackoverflow ausprobiert, aber sie haben nicht funktioniert. Einer von ihnen befindet sich unter dem Link (Akzeptierte Antwort dort) Alle ASP.NET-Web-API-Controller geben den Wert 404 zurück
Global.ASAX-Dateicode:
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 DATEI
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 }
);
}
}
Code der Konfigurationsdatei weiterleiten
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-Controller CODE
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)
{
}
}
}
AUCH ERHALTEN NUGET RESTORE FEHLER IN DER GLEICHEN LÖSUNGFehler Nuget konnte PNG nicht wiederherstellen
Sie müssen das Routing für web apiREGISTRIEREN, BEVOR SIEdas Routing für MVC registrieren. Daher sollte Ihre App_Start()
-Funktion im Wesentlichen so aussehen:
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);
}
Sie haben zwei Routen auf dieselbe Weise. Entfernen Sie entweder delete (id) oder get (id). Alternativ können Sie der Routenzuordnung eine Aktion hinzufügen.
sie können beide Konfigurationen wie folgt in dieselbe Datei schreiben
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 }
);
}
}
in der Datei "Global.aspx" Code wie folgt editieren--
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);
}