简体   繁体   中英

The batch request must have a “Content-Type” header / “multipart/mixed” as the media type

Got an error after I add the DefaultODataBatchHandler on my WEB API OData v4.

DefaultODataBatchHandler defaultODataBatchHandler = new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer); 

In browser I've got this error: The batch request must have a "Content-Type" header. In POSTMan I've got this error: The batch request must have 'multipart/mixed' as the media type.

If I didn't put the code above. I've got this error when accessing the $batch

"Message": "No HTTP resource was found that matches the request URI 'http://localhost:2288/$batch'.",
"MessageDetail": "No route providing a controller name was found to match request URI 'http://localhost:2288/$batch'"

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Got it now.

I just need to add CORS

        var cors = new EnableCorsAttribute(
            "*",
            "*",
            "*",
            "*"
        );

        config.EnableCors(cors);

And put wildcard for the exposed header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM