c# - GlobalConfiguration.Configure(WebApiConfig.Register) 和 Umbraco 的问题
问题描述
我有一个 Umbraco 项目和一个使用 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
orWebApiConfig.cs
文件 - 如果您从UmbracoApiController
or继承,路由将由 Umbraco 自动处理SurfaceController
。
阅读 our.umbraco.com 上的路由文档,特别是有关 WebApi 的部分:
https://our.umbraco.com/documentation/Reference/Routing/WebApi
基于此,您的 TestController 路由将如下所示:
~/Umbraco/Api/Test/GetAll
推荐阅读
- sql - 按列值查询某些行的联合和分组
- python - Argrelmin 不返回值
- imagemagick - Imagemagick - 检查顶行是否包含任何黑色像素
- javascript - 追加到从特定列开始的最后一行
- css - 视觉剪辑的不同角半径
- flutter - 在 Flutter 中,未来如何使用 Null Safety?
- python - Python异步等待问题
- android - 使用 Android BillingClient,如何在重新安装或更换设备后恢复用户的 IAP?
- powershell - 新手有多难?Powershell JSON 文件创建和网页抓取
- angular - 可编程搜索引擎 json Api