I have the Asp.Net Core MVC web application where I have the area called AdminUI.
My controller looks like this:
[Area("AdminUI")]
public class ConfigurationController : BaseController
{
public async Task<IActionResult> Client(int? id)
{
if (id == default)
{
var clientDto = _clientService.BuildClientViewModel();
return View(clientDto);
}
var client = await _clientService.GetClientAsync(id.Value);
client = _clientService.BuildClientViewModel(client);
return View(client);
}
}
And I have also following method for adding the feature to prefix whole area with some specific name:
public static IEndpointConventionBuilder MapIdentityServer4AdminUI(
this IEndpointRouteBuilder endpoint, string patternPrefix = "/")
{
return endpoint.MapAreaControllerRoute("AdminUI", "AdminUI",
patternPrefix + "{controller=Home}/{action=Index}/{id?}");
}
If I use this:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
ILoggerFactory loggerFactory)
{
app.UseRouting();
app.UseEndpoints(endpoint =>
{
endpoint.MapIdentityServer4AdminUI("/myPrefix/");
});
}
I can access the AdminUI area on following link:
https://localhost:43000/myPrefix/Configuration/Client
https://localhost:43000/myPrefix/Configuration/Client/2
It works well, but now I need restrict the URL above, that is possible to access only method Client with id which is number or without id parameter in URL. I do not know how to achieve, if I want to keep the prefix in URL above.
I have tried this solution, which does not work:
[Route("[area]/[controller]/[action]")]
[Route("[area]/[controller]/[action]/{id:int}")]
public async Task<IActionResult> Client(int id)
{
...
}
This works that I am able to use only ID like number and without ID, but this breaks the URL, because it ignores my prefix above.
URL is now:
https://localhost:43000/AdminUI/Configuration/Client
not:
https://localhost:43000/myPrefix/Configuration/Client
How can I achieve following behavior?