How to place routes with optional parameters Asp.net-core

Asked

Viewed 478 times

1

I wish my route was this way

 [HttpGet("APIPassword={APIPassword}/AvatarUUID={AvatarUUID}/Idioma?={Idioma}")]

Where Language is Optional

[HttpGet("APIPassword={APIPassword}/AvatarUUID={AvatarUUID}/{Idioma:string?}")]

But when I put the URL I really want to keep this format

Apipassword=blblb/Avataruuid=dfsafadsfasf/Language=en

The most concrete problem is that the Language can be entered

APIPassword=blblblb/AvatarUUID=dfsafadsfasf/Idioma=

And so I’m trying to solve the problem by making this parameter optional So I wanted to keep the Idiom= even if there is no value ahead

I wish it was possible to keep the url this way APIPassword=blblblb/AvatarUUID=dfsafadsfasf/Idioma=pt

But since Language may not bring value

APIPassword=blblblb/AvatarUUID=dfsafadsfasf/Idioma=

  • See if it works that way Idioma={lang?}

  • Post as a response

  • But I wanted the path to be Apipassword=blblblb/Avataruuid=dfsafadsfasf/Language=

1 answer

1

Just use the question mark in the parameter which can be optional. In the example below the id would be optional.

app.UseMvc(routes =>
{
    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
  • But I wanted to keep the path this way Apipassword=blblblb/Avataruuid=dfsafadsfasf/Language=

Browser other questions tagged

You are not signed in. Login or sign up in order to post.