ASP.NET Core의 get 메서드에 여러 매개 변수를 전달하는 방법
MVC 6 컨트롤러의 Get 메서드에 여러 매개 변수를 어떻게 전달할 수 있습니까? 예를 들어 다음과 같은 것을 가질 수 있기를 원합니다.
[Route("api/[controller]")]
public class PersonController : Controller
{
public string Get(int id)
{
}
public string Get(string firstName, string lastName)
{
}
public string Get(string firstName, string lastName, string address)
{
}
}
그래서 나는 같은 쿼리를 할 수 있습니다.
api/person?id=1
api/person?firstName=john&lastName=doe
api/person?firstName=john&lastName=doe&address=streetA
다음을 사용할 수도 있습니다.
// GET api/user/firstname/lastname/address
[HttpGet("{firstName}/{lastName}/{address}")]
public string GetQuery(string id, string firstName, string lastName, string address)
{
return $"{firstName}:{lastName}:{address}";
}
하나의 컨트롤러 동작 만 사용하지 않는 이유는 무엇입니까?
public string Get(int? id, string firstName, string lastName, string address)
{
if (id.HasValue)
GetById(id);
else if (string.IsNullOrEmpty(address))
GetByName(firstName, lastName);
else
GetByNameAddress(firstName, lastName, address);
}
또 다른 옵션은 속성 라우팅을 사용하는 것이지만 다른 URL 형식이 필요합니다.
//api/person/byId?id=1
[HttpGet("byId")]
public string Get(int id)
{
}
//api/person/byName?firstName=a&lastName=b
[HttpGet("byName")]
public string Get(string firstName, string lastName, string address)
{
}
URL에서 검색 매개 변수를 구문 분석하려면 다음과 같이 컨트롤러 메소드 매개 변수에를 주석으로 추가해야합니다 [FromQuery]
.
[Route("api/person")]
public class PersonController : Controller
{
[HttpGet]
public string GetById([FromQuery]int id)
{
}
[HttpGet]
public string GetByName([FromQuery]string firstName, [FromQuery]string lastName)
{
}
[HttpGet]
public string GetByNameAndAddress([FromQuery]string firstName, [FromQuery]string lastName, [FromQuery]string address)
{
}
}
가장 쉬운 방법은 단순히 AttributeRouting
.
[Route("api/YOURCONTROLLER/{paramOne}/{paramTwo}")]
public string Get(int paramOne, int paramTwo)
{
return "The [Route] with multiple params worked";
}
NB-I 제거 FromURI. 여전히 URL에서 값을 전달하고 결과를 얻을 수 있습니다. fromuri를 사용하는 benfifts를 아는 사람이 있으면 알려주십시오.
다른 답변 후 귀하의 의견에서 요청한 오버로딩에 대한 자세한 내용을 추가하려면 여기에 요약이 있습니다. 주석은 ApiController
각 GET
쿼리에서 어떤 작업이 호출되는지 보여줍니다 .
public class ValuesController : ApiController
{
// EXPLANATION: See the view for the buttons which call these WebApi actions. For WebApi controllers,
// there can only be one action for a given HTTP verb (GET, POST, etc) which has the same method signature, (even if the param names differ) so
// you can't have Get(string height) and Get(string width), but you can have Get(int height) and Get(string width).
// It isn't a particularly good idea to do that, but it is true. The key names in the query string must match the
// parameter names in the action, and the match is NOT case sensitive. This demo app allows you to test each of these
// rules, as follows:
//
// When you send an HTTP GET request with no parameters (/api/values) then the Get() action will be called.
// When you send an HTTP GET request with a height parameter (/api/values?height=5) then the Get(int height) action will be called.
// When you send an HTTP GET request with a width parameter (/api/values?width=8) then the Get(string width) action will be called.
// When you send an HTTP GET request with height and width parameters (/api/values?height=3&width=7) then the
// Get(string height, string width) action will be called.
// When you send an HTTP GET request with a depth parameter (/api/values?depth=2) then the Get() action will be called
// and the depth parameter will be obtained from Request.GetQueryNameValuePairs().
// When you send an HTTP GET request with height and depth parameters (/api/values?height=4&depth=5) then the Get(int height)
// action will be called, and the depth parameter would need to be obtained from Request.GetQueryNameValuePairs().
// When you send an HTTP GET request with width and depth parameters (/api/values?width=3&depth=5) then the Get(string width)
// action will be called, and the depth parameter would need to be obtained from Request.GetQueryNameValuePairs().
// When you send an HTTP GET request with height, width and depth parameters (/api/values?height=7&width=2&depth=9) then the
// Get(string height, string width) action will be called, and the depth parameter would need to be obtained from
// Request.GetQueryNameValuePairs().
// When you send an HTTP GET request with a width parameter, but with the first letter of the parameter capitalized (/api/values?Width=8)
// then the Get(string width) action will be called because the case does NOT matter.
// NOTE: If you were to uncomment the Get(string height) action below, then you would get an error about there already being
// a member named Get with the same parameter types. The same goes for Get(int id).
//
// ANOTHER NOTE: Using the nullable operator (e.g. string? paramName) you can make optional parameters. It would work better to
// demonstrate this in another ApiController, since using nullable params and having a lot of signatures is a recipe
// for confusion.
// GET api/values
public IEnumerable<string> Get()
{
return Request.GetQueryNameValuePairs().Select(pair => "Get() => " + pair.Key + ": " + pair.Value);
//return new string[] { "value1", "value2" };
}
//// GET api/values/5
//public IEnumerable<string> Get(int id)
//{
// return new string[] { "Get(height) => height: " + id };
//}
// GET api/values?height=5
public IEnumerable<string> Get(int height) // int id)
{
return new string[] { "Get(height) => height: " + height };
}
// GET api/values?height=3
public IEnumerable<string> Get(string height)
{
return new string[] { "Get(height) => height: " + height };
}
//// GET api/values?width=3
//public IEnumerable<string> Get(string width)
//{
// return new string[] { "Get(width) => width: " + width };
//}
// GET api/values?height=4&width=3
public IEnumerable<string> Get(string height, string width)
{
return new string[] { "Get(height, width) => height: " + height + ", width: " + width };
}
}
궁금한 경우를 대비하여 단일 경로 만 필요합니다.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
이 MVC보기 나 비슷한 것으로 모든 것을 테스트 할 수 있습니다. 예, JavaScript와 마크 업을 혼합해서는 안되며 평소처럼 부트 스트랩을 사용하지 않는다는 것을 알고 있지만 이것은 데모 목적으로 만 사용됩니다.
<div class="jumbotron">
<h1>Multiple parameters test</h1>
<p class="lead">Click a link below, which will send an HTTP GET request with parameters to a WebAPI controller.</p>
</div>
<script language="javascript">
function passNothing() {
$.get("/api/values", function (data) { alert(data); });
}
function passHeight(height) {
$.get("/api/values?height=" + height, function (data) { alert(data); });
}
function passWidth(width) {
$.get("/api/values?width=" + width, function (data) { alert(data); });
}
function passHeightAndWidth(height, width) {
$.get("/api/values?height=" + height + "&width=" + width, function (data) { alert(data); });
}
function passDepth(depth) {
$.get("/api/values?depth=" + depth, function (data) { alert(data); });
}
function passHeightAndDepth(height, depth) {
$.get("/api/values?height=" + height + "&depth=" + depth, function (data) { alert(data); });
}
function passWidthAndDepth(width, depth) {
$.get("/api/values?width=" + width + "&depth=" + depth, function (data) { alert(data); });
}
function passHeightWidthAndDepth(height, width, depth) {
$.get("/api/values?height=" + height + "&width=" + width + "&depth=" + depth, function (data) { alert(data); });
}
function passWidthWithPascalCase(width) {
$.get("/api/values?Width=" + width, function (data) { alert(data); });
}
</script>
<div class="row">
<button class="btn" onclick="passNothing();">Pass Nothing</button>
<button class="btn" onclick="passHeight(5);">Pass Height of 5</button>
<button class="btn" onclick="passWidth(8);">Pass Width of 8</button>
<button class="btn" onclick="passHeightAndWidth(3, 7);">Pass Height of 3 and Width of 7</button>
<button class="btn" onclick="passDepth(2);">Pass Depth of 2</button>
<button class="btn" onclick="passHeightAndDepth(4, 5);">Pass Height of 4 and Depth of 5</button>
<button class="btn" onclick="passWidthAndDepth(3, 5);">Pass Width of 3 and Depth of 5</button>
<button class="btn" onclick="passHeightWidthAndDepth(7, 2, 9);">Pass Height of 7, Width of 2 and Depth of 9</button>
<button class="btn" onclick="passHeightWidthAndDepth(7, 2, 9);">Pass Height of 7, Width of 2 and Depth of 9</button>
<button class="btn" onclick="passWidthWithPascalCase(8);">Pass Width of 8, but with Pascal case</button>
</div>
param1 / undefined / pram3과 같은 URL이 프런트 엔드 (예 : Angular)에서 생성되는 경우에는 param1 / param2 / param3 스타일의 URL을 사용하는 것이 실패하므로 고전적인 접근 방식을 사용했습니다.
[HttpGet("params/{prms}")]
public async Task<ActionResult<IEnumerable<SomeType>>> GetDataByParams(string prms)
{
string[] paramsArray = prms.Split("===");//Separate params in one string
string param1= paramsArray[0].Trim();
string param2= paramsArray[1].Trim();
string param3= paramsArray[2].Trim();
...
}
결과 URL은 다음과 같습니다. http : // localhost : 51975 / api / YourController / params / PARAM1 === PARAM2 ..
public HttpResponseMessage Get(int id,string numb)
{
using (MarketEntities entities = new MarketEntities())
{
var ent= entities.Api_For_Test.FirstOrDefault(e => e.ID == id && e.IDNO.ToString()== numb);
if (ent != null)
{
return Request.CreateResponse(HttpStatusCode.OK, ent);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Applicant with ID " + id.ToString() + " not found in the system");
}
}
}
다음을 수행 할 수 있습니다.
[HttpGet]
public async Task<IActionResult> GetAsync()
{
string queryString = Request.QueryString.ToString().ToLower();
return Ok(await DoMagic.GetAuthorizationTokenAsync(new Uri($"https://someurl.com/token-endpoint{queryString}")));
}
각 요소에 개별적으로 액세스해야하는 경우을 참조하십시오 Request.Query
.
'developer tip' 카테고리의 다른 글
Homebrew 업데이트 실패 :“제발, 변경 사항을 커밋하거나 병합하기 전에 숨김” (0) | 2020.11.17 |
---|---|
Websocket onerror-오류 설명을 읽는 방법? (0) | 2020.11.17 |
ReSharper는 메서드가 정적 일 수 있지만 그렇지 않은 경우 불평합니다. (0) | 2020.11.17 |
두 페이지로드시 Rails 플래시 메시지가 유지됨 (0) | 2020.11.17 |
급행 경로 매개 변수 조건 (0) | 2020.11.17 |