GlobalConfiguration.Configure(WebApiConfig.Register) 和 Umbraco 的问题
Problem with GlobalConfiguration.Configure(WebApiConfig.Register) and Umbraco
我有一个 Umbraco 项目和 1 个使用 Umbraco 的 WebApi 项目。我已经安装了第二个 UmbracoCms.Core 并且我有一个控制器是 UmbracoAppiController
public class TestController : UmbracoApiController
{
[HttpGet]
public IEnumerable<string> GetAll()
{
return new string[] { "value1", "value2" };
}
}
我有 global.asax.cs:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
和WebApiConfig.cs:
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 }
);
}
}
当我尝试进行 WebApi 调用时,我得到:
Server Error in '/' Application.
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 17: // Code that runs on application startup Line 18: AreaRegistration.RegisterAllAreas(); Line 19: GlobalConfiguration.Configure(WebApiConfig.Register); Line 20: RouteConfig.RegisterRoutes(RouteTable.Routes); Line 21: }
Source File: C:\Projects\SBSADeviceShop5\ShopApi1\Global.asax.cs Line: 19
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] Umbraco.Web.WebApi.UnhandledExceptionLogger..ctor() +12 Umbraco.Web.WebApi.UnhandedExceptionLoggerConfigurationAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) +66 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +188 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +62 System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType) +130 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()
+568 System.Lazy`1.CreateValue() +708 System.Lazy`1.LazyInitValue() +184 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()
+18 System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider) +76 System.Web.Http.Routing.<>c__DisplayClass1_1.<MapAttributeRoutes>b__1()
+75 System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer) +66 System.Web.Http.Routing.<>c__DisplayClass1_0.<MapAttributeRoutes>b__0(HttpConfiguration config) +125 ShopApi1.Global.Application_Start(Object sender, EventArgs e) in C:\Projects\SBSADeviceShop5\ShopApi1\Global.asax.cs:19
[HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +520 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +176 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +165 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +267 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +341
[HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +523 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
+107 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +688
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3160.0
您根本不需要执行 Global.asax.cs
或 WebApiConfig.cs
文件 - 如果您继承自 UmbracoApiController
或 [=,路由将由 Umbraco 自动处理14=].
阅读 our.umbraco.com 上的路由文档,特别是有关 WebApi 的部分:
https://our.umbraco.com/documentation/Reference/Routing/WebApi
基于此,您的 TestController 路由将如下所示:
~/Umbraco/Api/Test/GetAll
我有一个 Umbraco 项目和 1 个使用 Umbraco 的 WebApi 项目。我已经安装了第二个 UmbracoCms.Core 并且我有一个控制器是 UmbracoAppiController
public class TestController : UmbracoApiController
{
[HttpGet]
public IEnumerable<string> GetAll()
{
return new string[] { "value1", "value2" };
}
}
我有 global.asax.cs:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
和WebApiConfig.cs:
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 }
);
}
}
当我尝试进行 WebApi 调用时,我得到:
Server Error in '/' Application.
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 17: // Code that runs on application startup Line 18: AreaRegistration.RegisterAllAreas(); Line 19: GlobalConfiguration.Configure(WebApiConfig.Register); Line 20: RouteConfig.RegisterRoutes(RouteTable.Routes); Line 21: }
Source File: C:\Projects\SBSADeviceShop5\ShopApi1\Global.asax.cs Line: 19
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] Umbraco.Web.WebApi.UnhandledExceptionLogger..ctor() +12 Umbraco.Web.WebApi.UnhandedExceptionLoggerConfigurationAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) +66 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +188 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +62 System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType) +130 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()
+568 System.Lazy`1.CreateValue() +708 System.Lazy`1.LazyInitValue() +184 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()
+18 System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider) +76 System.Web.Http.Routing.<>c__DisplayClass1_1.<MapAttributeRoutes>b__1()
+75 System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer) +66 System.Web.Http.Routing.<>c__DisplayClass1_0.<MapAttributeRoutes>b__0(HttpConfiguration config) +125 ShopApi1.Global.Application_Start(Object sender, EventArgs e) in C:\Projects\SBSADeviceShop5\ShopApi1\Global.asax.cs:19
[HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +520 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +176 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +165 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +267 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +341
[HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +523 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
+107 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +688
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3160.0
您根本不需要执行 Global.asax.cs
或 WebApiConfig.cs
文件 - 如果您继承自 UmbracoApiController
或 [=,路由将由 Umbraco 自动处理14=].
阅读 our.umbraco.com 上的路由文档,特别是有关 WebApi 的部分:
https://our.umbraco.com/documentation/Reference/Routing/WebApi
基于此,您的 TestController 路由将如下所示:
~/Umbraco/Api/Test/GetAll