Home > Back-end >  Pass url param in route
Pass url param in route

Time:09-21

I have this controller with one method which receives two variables. One is the culture, the other is a path.

public class WebsiteContentController : ApiController
{
    [HttpGet]
    public HttpResponseMessage Test(string culture, string path)
    {
        var json = WebsiteContentManager.GetContent();

        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(json, Encoding.UTF8, "application/json");
        return response;
    }
}

Which i call like in this examples:

/Api/WebsiteContent/Test?path=/quem-somos/a-marca/&culture=pt
/Api/WebsiteContent/Test?path=/quem-somos/&culture=pt
/Api/WebsiteContent/Test?path=/quem-somos/something/something/&culture=pt

What i you like to do, is to create a endpoint to similar to this one:

/Api/WebsiteContent/Test/pt/quem-somos/a-marca/

Is there any way to call an endpoint with this format?

CodePudding user response:

only like this route can be used for url ".../Api/WebsiteContent/Test/pt/quem-somos/a-marca"

[HttpGet("{culture/path1/path2}")]
public HttpResponseMessage Test(string culture, string path1, string path2)
{
  var path="/"  path1 "/" path2 "/";
}

or maybe like this for the flexible path

[HttpGet("{culture/path1?/path2?/path3?/patch4?}")]
public HttpResponseMessage Test(string culture, string path1, string path2
string path3, string path4)

CodePudding user response:

You can use the RouteAttribute template.

It would look something like this.

[Route("API/V1/{varible1}/{varible2}")]
public async Task<string> APIAsync(string varible1, string varible2)
{
    //do things here
}

You can make the route as custom as you want grabbing the variables you need and adding static route works like API/V1 that I have above.

  • Related