Search code examples
asp.net-corejsonpasp.net-core-mvckestrel-http-server

ASP.NET Core MVC with JSONP


I would like to enable existing MVC controllers (from ASP.NET Core/Kestrel server) to wrap messages as JSONP so they can be accessible cross-domain from browser. What are my options?


Solution

  • JSONP is pretty much deprecated, since most frameworks and servers support CORS, which makes JSONP obsolete (it doesn't work well with anything other then GET requests).

    // ConfigureServices
            services.AddCors(options =>
            {
                options.AddPolicy("AnyOrigin", builder =>
                {
                    builder
                        .AllowAnyOrigin()
                        .AllowAnyMethod();
                });
            });
    
    // Configure
    app.UseCors("AnyOrigin");
    

    This will basically allow ajax call from any domain. If you need more fine-grained control over domains and actions, check out the official docs.