我对此感到疯狂。
我正在使用Visual Studio 2012 Premium,.NET Framework 4.5.1和C#开发ASP.NET Web Api 2.2项目。
我创建了一个空的ASP.NET MVC 5
项目。我已删除Global.asax
并创建此Startup.cs
类:
using Microsoft.Owin;
using Ninject;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;
using System.Reflection;
using System.Web.Http;
using System.Web.Routing;
using MyProject.Web.API.App_Start;
[assembly: OwinStartup(typeof(MyProject.Web.API.Startup))]
namespace MyProject.Web.API
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
var webApiConfiguration = new HttpConfiguration();
webApiConfiguration.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
webApiConfiguration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
webApiConfiguration.Routes.MapHttpRoute(
name: "ProductionOrderActionApi",
routeTemplate: "api/MyProductionOrders/{orderNumber}/{action}",
defaults: new { controller = "MyProductionOrders" });
app.UseNinjectMiddleware(CreateKernel);
app.UseNinjectWebApi(webApiConfiguration);
}
private static StandardKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
var containerConfigurator = new NinjectConfigurator();
containerConfigurator.Configure(kernel);
}
}
}
该项目适用于ApiController个类,但当我尝试访问Controller时,我收到HTTP 404状态代码:找不到。
我需要做些什么才能允许网页?我认为问题出在Routes
,但我尝试将RouteConfig
添加到项目中,但我不知道如何。
我在Google上搜索过很多但我没有找到与我的问题有关的任何内容(或者我没有提供正确的搜索字词)。
如果您需要NinjectConfigurator
课程,请告诉我并添加。
答案 0 :(得分:3)
您需要使用MapRoute作为控制器。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
MapRoute是一种扩展方法,其中MvcRouteHandler被设置为请求的路由处理程序。如果你没有映射一个给定的路由要由MvcRouteHandler处理,那么你就不会使用导致控制器被实例化的Mvc请求处理管道。
MapRoute使用MvcRouteHandler
public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
{
if (routes == null)
throw new ArgumentNullException("routes");
if (url == null)
throw new ArgumentNullException("url");
Route route = new Route(url, (IRouteHandler) new MvcRouteHandler())
{
Defaults = RouteCollectionExtensions.CreateRouteValueDictionaryUncached(defaults),
Constraints = RouteCollectionExtensions.CreateRouteValueDictionaryUncached(constraints),
DataTokens = new RouteValueDictionary()
};
ConstraintValidation.Validate(route);
if (namespaces != null && namespaces.Length > 0)
route.DataTokens["Namespaces"] = (object) namespaces;
routes.Add(name, (RouteBase) route);
return route;
}
MapHttpRoute使用HttpMessageHandler:
public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler)
{
if (routes == null)
throw Error.ArgumentNull("routes");
HttpRouteValueDictionary routeValueDictionary1 = new HttpRouteValueDictionary(defaults);
HttpRouteValueDictionary routeValueDictionary2 = new HttpRouteValueDictionary(constraints);
IHttpRoute route = routes.CreateRoute(routeTemplate, (IDictionary<string, object>) routeValueDictionary1, (IDictionary<string, object>) routeValueDictionary2, (IDictionary<string, object>) null, handler);
routes.Add(name, route);
return route;
}
答案 1 :(得分:0)
对我而言,确实没有设置路由设置,因为您只定义了webapi的路由。 mvc和webapi的路由配置略有不同,因此您无法像在那里那样设置两者的路由。 从我正在阅读的书中引用:
避免框架之间冲突的关键是谨慎的路线 建立;为方便起见,ASP.NET Web API默认会占用URI / api下的空格,而所有其他根级URL都是 由MVC处理。通常,Web API路由在中定义 WebApiConfig针对HttpConfiguration对象的静态类及其 路由属性,而MVC路由在静态RouteConfig中定义 class,直接针对System.Web.RouteCollection。
//Web API routing configuration
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
//MVC routing configuration
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 }
);
}
}