0

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 result = DoStuff(culture, path);

        return result;
    }
}

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?

2 Answers 2

0

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)

if you use old net version add MapMvcAttributeRoutes to RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
 
    routes.MapMvcAttributeRoutes();
 
    routes.MapRoute(
        name: “Default”,
        url: “api/{controller}/{action}/{id}”,
        defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion. I was thinking on only passing two variables. But maybe i will consider this solution.
the HttpGet contructor im my code does not accept that string. Im using .net framework 4 and web api. What are you using that allow to pass that string?
@LucaCosta I updated my answer. pls try again.
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.