我是ASP Net的新手,在将URL作为简单参数传递时遇到了一些麻烦。 这些是我尝试过的解决方案:
[ApiController]
[Route("api/[controller]")]
public class LoginRedditController : ControllerBase
{
[HttpGet("{*longUrl}")]
public ActionResult<string> Get(string longUrl)
{
Console.WriteLine(longUrl);
return "OK";
}
}
这是我尝试调用的URL:
http://localhost:5001/LoginReddit?longUrl=https://www.reddit.com/r/playmygame/comments/glftsj/stickn_roll_collect_everything_as_you_roll/
我也尝试对URL进行编码,但结果是相同的:我得到了“ ERR_EMPTY_RESPONSE” 这是我的第二次尝试:
[ApiController]
[Route("api/[controller]")]
public class LoginRedditController : ControllerBase
{
[HttpGet]
public ActionResult<string> Get([FromQuery]string url)
{
Console.WriteLine(longUrl);
return "OK";
}
}
我使用了以前使用的相同URL,但得到了相同结果。
在我尝试的尝试中,我像这样更改了路线:
[Route("api/[controller]/{url}")]
public class LoginRedditController : ControllerBase
{
[HttpGet]
public ActionResult<string> Get([FromQuery]string url)
{
Console.WriteLine(longUrl);
return "OK";
}
}
我尝试使用以下网址:
http://localhost:5001/LoginReddit/https://www.reddit.com/r/playmygame/comments/glftsj/stickn_roll_collect_everything_as_you_roll/
也编码,但我得到了相同的结果。
有问题的人在哪里?
对于这样的URL:
您可以这样编写控制器操作:
The
longUrl
string value is automatically taken from the query arguments passed to the route. And the route for the action is combined from that[Route]
attribute on the controller and the[HttpGet]
attribute on the action.