2

I'm seeing a lot of the same exceptions in Application Insights for HEAD requests for many paths on my website:

System.ArgumentException: The leading '?' must be included for a non-empty query. (Parameter 'value')

The website is running in Azure App Service. When I debug locally and hit those same URLs with a HEAD request, it returns a 405 Not Implemented, but no exceptions. Perhaps because local is using Kestrel and Azure is using IIS?

I am specifying routes like this:

endpoints.MapControllerRoute(
    name: "home-test",
    pattern: "home/test",
    defaults: new { controller = "Home", action = "Test" });

endpoints.MapControllerRoute(
    name: "home-test-id",
    pattern: "home/test/{id}",
    defaults: new { controller = "Home", action = "Test" });

And my controller action looks like this:

[HttpGet]
public async Task<IActionResult> Test(string id)

What's the best way to resolve these exceptions?

Mack Male
  • 77
  • 1
  • 6
  • Has your problem been solved? – Jason Pan Jul 14 '20 at 06:10
  • Sorry for the delay. Yes, this did fix the problem I was seeing. I now have just the default route in UseEndpoints, and I moved the rest to attribute routing as suggested. Thank you! – Mack Male Jul 14 '20 at 21:49

1 Answers1

0

The routing rules are executed sequentially from top to bottom. After my testing, I think your default route part should not be modified.

The code has been modified so that it should run normally.

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");


            endpoints.MapControllerRoute(
                name: "home-test-id",
                pattern: "home/test/{id}",
                defaults: new { controller = "Home", action = "Index" });
        });

In addition, I suggest you use custom routing rules.

namespace webapi.Controllers
{
    [Route("Home1")]
    public class MyController:Controller
    {
        [Route("test/{id}")]
        public string Get(int id)
        {
            return "id: " + id;
        }
    }
}
Jason Pan
  • 15,263
  • 1
  • 14
  • 29