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