如何在 .NET 6 中使用带破折号 (-) 的路由器参数?
How do I use a router parameter with dash (-) in .NET 6?
在下面的代码中,如何将 app-name 值赋给 ApplicationName?
[ApiController]
[Route("testcontroller/app-name")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules(string ApplicationName)
{
throw new NotImplementedException($"/app/{ApplicationName}/modules not implemented");
}
}
按照惯例(没有破折号)...
[ApiController]
[Route("testcontroller/{appName}")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules([FromRoute]string appName)
{
throw new NotImplementedException($"/app/{appName}/modules not implemented");
}
}
或者通过在 FromRoute 属性上使用名称 属性:
[ApiController]
[Route("testcontroller/{app-name}")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules([FromRoute(Name = "app-name")]string appName)
{
throw new NotImplementedException($"/app/{appName}/modules not implemented");
}
}
在下面的代码中,如何将 app-name 值赋给 ApplicationName?
[ApiController]
[Route("testcontroller/app-name")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules(string ApplicationName)
{
throw new NotImplementedException($"/app/{ApplicationName}/modules not implemented");
}
}
按照惯例(没有破折号)...
[ApiController]
[Route("testcontroller/{appName}")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules([FromRoute]string appName)
{
throw new NotImplementedException($"/app/{appName}/modules not implemented");
}
}
或者通过在 FromRoute 属性上使用名称 属性:
[ApiController]
[Route("testcontroller/{app-name}")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules([FromRoute(Name = "app-name")]string appName)
{
throw new NotImplementedException($"/app/{appName}/modules not implemented");
}
}