网址以&结束时,.NET WebApi会中断 [英] .NET WebApi breaks when url ends with ampersand

查看:76
本文介绍了网址以&结束时,.NET WebApi会中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ApiController-例如'Home'控制器,其动作'Test'接受两个参数-test1和test2,均具有默认值

I have an ApiController - for example 'Home' controller, with action 'Test' which accepts two parameters - test1 and test2, both with default values

[System.Web.Http.HttpGet]
public ActionResult Test(int test1 = 3, int test2 = 5)
{
    var a = 0;
    return null;
}

现在,当我打电话给 Home/Test?test1 = 1 时,一切都很好.
但是当我呼叫 Home/Test?test1 = 1& 时,服务器会引发异常

Now when I call Home/Test?test1=1, everything is OK.
But when I call Home/Test?test1=1&, the server throws exception

参数字典为方法的非空类型'System.Int32'的参数'test2'包含一个空条目 'System.Web.Mvc.ActionResult测试(Int32,Int32)' 'TestAPI.Controllers.ValuesController'.可选参数必须是 引用类型,可为null的类型,或被声明为可选的 参数.

The parameters dictionary contains a null entry for parameter 'test2' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Test(Int32, Int32)' in 'TestAPI.Controllers.ValuesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

我的路由配置当前如下:

My route configuration is currently like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

这也发生在全新的WebApi项目上,因此这与我的配置无关.
最大的问题是,&"号来自一个客户项目,尽管我知道这是一个错误的请求,但我无法在此处更改它.

This also happens on a brand new WebApi project so it is not something from my configuration.
The big problem is that this ampersand comes from a client project and I cannot change it there, although I am aware that this is a bad request.

最奇怪的是,如果Home控制器继承了Controller而不是ApiController,那么一切都很好.

The strangest thing is that if Home controller is inheriting Controller instead of ApiController - everything is fine.

编辑1
我忘了提一下,如果我将参数设置为可为空(即int?),则错误消失了,但是当调用错误的URL时,test2的值为null而不是5,所以这不是我的选择.

Edit 1
I forgot to mention that if I make the parameters nullable (i.e. int?) then the error dissapears but test2 has value of null instead of 5 when calling the bad URL, so this is not an option for me.

推荐答案

您可以使用自定义DelegatingHandler并删除结尾的'&':

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);
    }
}

Application_Start中注册新的处理程序:

Register the new Handler in Application_Start:

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

或将其添加到您的HttpConfiguration(然后只能通过Webapi调用来调用):

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());
    ...   
}

这篇关于网址以&结束时,.NET WebApi会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆