Global.asax中的WebApiConfig错误

本文关键字:错误 WebApiConfig 中的 asax Global | 更新日期: 2023-09-27 18:07:37

请看一下我的Global.asax,我把"问题"大写。当我尝试构建时,我会得到"The name 'WebApiConfig' does not exist in the current context

我缺少什么,我添加了nuget Microsoft.AspNet.WebApi.WebHost

using SportsStore.WebUI.Infrastructure;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace SportsStore.WebUI
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WEBAPICONFIG.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            ControllerBuilder.Current.SetControllerFactory(new
            NinjectControllerFactory());
        }
    }
}

Global.asax中的WebApiConfig错误

如果您在visual studio中创建新的ASP.NET Web应用程序时选择添加Web API引用,则WebApiConfig类通常在App_Start下创建。

如果类不存在,请手动添加:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}