[.net Core] 解決CROS 問題

之前已經講了什麼是CROS, 在此不再論及, 在本篇會示範如何在.net Core 上設定CROS.在startup.cs 中修改method configurateService() 如下.

public void ConfigureServices(IServiceCollection services)
        {
            / Enable CROS.
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAllOrigin", builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyMethod();
                    builder.AllowAnyHeader();
                });
            });
        }

在controller method 或直接在controller 上, 加入以下annotation, 而其中AllowAllOrigin 須相同.

[EnableCors("AllowAllOrigin")]
    public class TestController : ControllerBase
    {
}

 

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.