0

Lets say we have an API that receives URLs in one of its controllers actions. How is this doable in Net Core 3.1? If i understand correctly, then https://www.test.com/anothertest/test will mess up the routing to the controller?

Code example below

[Route("api/[controller]")]
public class TestController : Controller
{
    [HttpPost("{url}")]
    public ActionResult<string> Post(string url)
    {
        // I want this to work! The url should be the full url specified i.e. https://www.myawesome.com/url/with/slashes
    }
}

So if i call https://localhost:5001/api/Test/https://www.url.com/with/slashes i would get https://www.url.com/with/slashes as the incoming url argument.

1
  • Hi @Tomato,do you want to get the url string in action? Commented Jan 6, 2021 at 1:31

1 Answer 1

1

If you pass https://www.url.com/with/slashes as part of the url,"/" will be unrecognized.

Normally, the easiest way to pass the url is through querystring.

You can change your code like bellow.

    [HttpPost]
    public ActionResult<string> Post(string url)
    {
        //...
    }

Then the url should be

https://localhost:xxxxx/api/test/?url=https://www.url.com/with/slashes
Sign up to request clarification or add additional context in comments.

1 Comment

Well, that's what i wanted to avoid :(. One small fix is that the url will be https://localhost:xxxxx/api/test?url=https://www.url.com/with/slashes

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.