Global.asax中的WebApiConfig错误

时间:2014-06-08 13:44:18

标签: c#

请看看我的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());
        }
    }
}

1 个答案:

答案 0 :(得分:5)

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

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

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();
    }
}