Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 418
Karol Kolenda Avatar asked Oct 31 '22 00:10

Karol Kolenda


1 Answers

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.

like image 200
Tseng Avatar answered Nov 11 '22 15:11

Tseng