在查询字符串

时间:2015-09-08 19:33:02

标签: c# asp.net asp.net-web-api cors owin

我在这里有一种奇怪而奇怪的情绪。

如果我向/scenes/?lang=es-ar发送请求,标题设置正常,一切似乎都正常。

Headers are present

但是,如果我向/scenes/?lang=es-ar&sort.asc=creation发送一个,则标题丢失,我因为交叉来源而无法获取回复。

Headers are missing

CORS由Owin的CORS中间件自动管理,所以这不在我手中。

这是我的中间件配置:

private void ConfigureCors(IAppBuilder application)
{
    CorsPolicy policy = new CorsPolicy()
    {
        AllowAnyHeader = true,
        AllowAnyOrigin = true,
        SupportsCredentials = true,
    };

    policy.Methods.Add("GET");
    policy.Methods.Add("POST");
    policy.Methods.Add("PUT");
    policy.Methods.Add("DELETE");
    policy.Methods.Add("OPTIONS");

    policy.ExposedHeaders.Add("Location");

    application.UseCors(new CorsOptions()
    {
        PolicyProvider = new CorsPolicyProvider()
        {
            PolicyResolver = request => System.Threading.Tasks.Task.FromResult(policy)
        }
    });
}

为什么没有在回复中发送标题?

我猜测它与"."

中的sort.asc=creation(点)有关

我使用最新版本的 ASP.NET Web Api 5.2.3 )和 Microsoft.Owin.Cors 3.0.1

1 个答案:

答案 0 :(得分:2)

托管服务器IIS可能会拦截飞行前请求。要确保ASP.NET处理OPTION请求,请在web.config中添加以下内容:

public class MyBraoadCastReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {


            Intent eventService = new Intent(context,  CheckingService.class);
            context.startService(eventService);

    }

}

在我看来,如果您计划将IIS用作托管环境,那么将<receiver android:name=".SchedulerReciever" android:process=":my_process"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> <service android:name=".CheckingService" android:process=":my_process" > </service> <receiver android:name=".MyBraoadCastReciever" android:process=":my_process" android:enabled="true" android:exported="false" > </receiver> 用于WebAPI并不是一个不错的选择。 Asp.net WebAPi的更好选择是Asp.net web API implementation of CORS