I wanna force the Route
with .html and a 32 length id.
For example, here is the URL:
https://localhost:44331/Re/test.html?id=12345678901234567890123456789012
I want it when there is no id parameter in the URL or the length of id is not 32, it returns 404 status code.
Here is the controller:
namespace V.Controllers
{
[Route("Re/")]
public class ReController : Controller
{
[Route("test.html{id:length(32)}")]
public IActionResult test(string id)
{
return View();
}
}
}
After I ran the code, it always reports 404 status code.
What's wrong with my route?
I don't think you can specify query string parameters in the route. Try validating the id
in the action, or if you can change the route, add it as an additional segment.
[Route("Re/")]
public class ReController : Controller
{
[Route("test.html")]
public IActionResult test(string id)
{
if (id == null || id.Length != 32)
return NotFound();
return Json(new {id= id});
}
[Route("test2.html/{id:length(32)}")]
public IActionResult test2(string id)
{
return Json(new {id= id});
}
}
See: Microsoft Docs