Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApiConfig error in Global.asax

Tags:

c#

Please have a look at my Global.asax, i put the "problem" in caps. When I try to build, I get "The name 'WebApiConfig' does not exist in the current context

What am I missing, I´ve added the 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());
        }
    }
}
like image 328
user2915962 Avatar asked Jun 08 '14 13:06

user2915962


People also ask

How to handle Error in Global asax?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.

What class gives access to Web API specific configuration details?

Web API configuration settings are defined in the HttpConfiguration class.


1 Answers

The WebApiConfig class is normally created under App_Start if you choose to add Web API references when creating a new ASP.NET web app in visual studio.

Manually add the class if it's not present:

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();
    }
}
like image 106
Rob Kraft Avatar answered Sep 23 '22 19:09

Rob Kraft