.NET WebApi breaks when url ends with ampersand

孤街浪徒 提交于 2019-12-01 22:09:00

You can use a custom DelegatingHandler and remove trailing '&':

public class SanitizeHandler : System.Net.Http.DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.RequestUri.ToString().EndsWith("&"))
            request.RequestUri = new Uri(request.RequestUri.ToString().TrimEnd('&'));

        return base.SendAsync(request, cancellationToken);
    }
}

Register the new Handler in Application_Start:

GlobalConfiguration.Configuration.MessageHandlers.Add(new SanitizeHandler());

Or add it to your HttpConfiguration(then it is only called with Webapi calls):

public static void Register(HttpConfiguration config)
{
    ...
    config.MessageHandlers.Add(new SanitizeHandler());
    ...   
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!