Tomato
Tomato

Reputation: 153

How to correctly receive string in url format in ASP NET Core 3.1?

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.

Upvotes: 0

Views: 352

Answers (1)

Yinqiu
Yinqiu

Reputation: 7180

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

Upvotes: 1

Related Questions