ASP.NET कोर में एक विधि प्राप्त करने के लिए कई मापदंडों को कैसे पास करें


107

मैं MVC 6 नियंत्रक में तरीके प्राप्त करने के लिए कई मापदंडों में कैसे पास कर सकता हूं। उदाहरण के लिए मैं निम्नलिखित की तरह कुछ करने में सक्षम होना चाहता हूँ।

[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

जवाबों:


91

आप इसका उपयोग भी कर सकते हैं:

// 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}";
}

नोट : कृपया metalheart के और का उल्लेख metalheartऔर Mark Hughesएक संभवतः बेहतर दृष्टिकोण के लिए।


21
जब तक आप सभी को एक ही अंतिम नाम के साथ प्राप्त करने की आवश्यकता है :)
फिलिप

14
यह एपीआई मार्गों को डिजाइन करने के लिए एक बहुत बुरा तरीका है ... बिल्कुल भी नहीं।
थॉमस लेवेस्क

7
उपरोक्त दृष्टिकोण बहुत ही बोझिल लग रहा है, नहीं मिलता है कि यह बहुत सारे upvotes क्यों है।
बर्नौली आईटी

1
@ThomasLevesque क्या आप इसका मतलब यह नहीं था जा रहा है?
ब्रूनो सैंटोस

2
@BrunoSantos यह REST के सिद्धांतों का पालन नहीं करता है। URI को संसाधनों की विशिष्ट पहचान के लिए माना जाता है। यह यहाँ नहीं है (एक ही पहले और अंतिम नाम के साथ कई व्यक्ति हो सकते हैं, और एक पते को निश्चित रूप से एक पहचानकर्ता नहीं माना जा सकता है)
थॉमस लेवेस्क

60

सिर्फ एक कंट्रोलर एक्शन का इस्तेमाल क्यों नहीं किया गया?

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)
{
}

हां, अब मैं इसे हल करता हूं कि सभी विशेषताओं में सिर्फ एक कार्रवाई का उपयोग करके मैं एक व्यक्ति को खोजना चाहता हूं। एक सामान्य खोज की तरह। मैं पसंद करूँगा, हालांकि अगर वहाँ एक नियंत्रक में अतिभारित कार्यों के लिए एक रास्ता है, लेकिन ऐसा नहीं हो सकता है।
मार्संड

3
यह .net कोर 2.0 के साथ काम नहीं करता है, क्योंकि कोई वैध यूआरएल टेम्पलेट वास्तव में उत्पन्न नहीं होता है।
ZZZ

44

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)
    {

    }
}

6
आपको इसकी आवश्यकता क्यों पड़ेगी? क्वेरी स्ट्रिंग से पैरामीटर बाइंडिंग डिफ़ॉल्ट रूप से होता है ...
मेटलहार्ट

मैंने दोनों की कोशिश की है, लेकिन मैं [FromQuery] के साथ या बिना असफल होने के लिए ओवरलोडिंग कर रहा हूं
mstrand

2
@mstrand मैंने अपडेट किया है - इसे दें, अतिरिक्त [HttpGet]एनोटेशन, अलग-अलग विधि के नाम और विशिष्ट मार्ग [Route]- इन मार्गों को पूरी तरह से स्पष्ट किया जाना चाहिए, जो कुछ संभावित समस्याओं को समाप्त करता है।
मार्क ह्यूजेस

9

मुझे लगता है कि सबसे आसान तरीका केवल उपयोग करना है AttributeRouting

[Route("api/YOURCONTROLLER/{paramOne}/{paramTwo}")]
    public string Get(int paramOne, int paramTwo)
    {
        return "The [Route] with multiple params worked";
    }

क्या मैं पसंदीदा संदर्भ प्रकार का उपयोग कर सकता हूं? अर्थात्,int paramOne, string paramTwo
k4s

यदि आप अपने दूसरे पैरामीटर को वैकल्पिक बनाना चाहते हैं तो [मार्ग ("एपीआई / योरकंट्रोलर / {पैरामाउंट} / {परमट्वू;}")] का उपयोग करें
Anytoe

8

मैं एक तर्क के रूप में एक अलग dto ऑब्जेक्ट का उपयोग करने का सुझाव दूंगा:

[Route("api/[controller]")]
public class PersonController : Controller
{
    public string Get([FromQuery] GetPersonQueryObject request)
    {
        // Your code goes here
    }
}

public class GetPersonQueryObject 
{
    public int? Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Address { get; set; }
}

डॉटनेट आपकी ऑब्जेक्ट को फ़ील्ड मैप करेगा।

इससे आपके मापदंडों से गुजरना बहुत आसान हो जाएगा और इसके परिणामस्वरूप बहुत स्पष्ट कोड होगा।


5

वेब एपीआई कोर में कई पैरामीटर के साथ कॉल करने के लिए

  [ApiController]
    [Route("[controller]")]
    public class testController : Controller
    {

      [HttpGet]
        [Route("testaction/{id:int}/{startdate}/{enddate}")]
        public IEnumerable<classname> test_action(int id, string startdate, string enddate)
        {

            return List_classobject;
        }

    }

In web browser
https://localhost:44338/test/testaction/3/2010-09-30/2012-05-01

3

किसी अन्य उत्तर के बाद आपकी टिप्पणी में आपके द्वारा पूछे गए ओवरलोडिंग के बारे में कुछ और विवरण जोड़ने के लिए, यहां एक सारांश दिया गया है। 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 दृश्य, या कुछ simlar के साथ यह सब परीक्षण कर सकते हैं। हां, मुझे पता है कि आपको जावास्क्रिप्ट को मार्कअप के साथ नहीं मिलाना चाहिए और मैं बूटस्ट्रैप का उपयोग नहीं करूंगा जैसे आप सामान्य रूप से करते हैं, लेकिन यह केवल डेमो उद्देश्यों के लिए है।

<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>

1

यहां छवि विवरण दर्ज करें

NB-I ने FromURI हटा दिया। क्या मैं URL से मान पास कर सकता हूं और परिणाम प्राप्त कर सकता हूं।


जैसा कि पैरामीटर बाइंडिंग [1] सरल प्रकार के लिए प्रलेखन में निर्धारित किया गया है, "(int, bool, double, और आगे), plus TimeSpan, DateTime, गाइड, दशमलव और स्ट्रिंग" स्वचालित रूप से यूआरआई को पढ़ा जाएगा। विशेषता [FromURI] की आवश्यकता तब होती है जब पैरामीटर इन प्रकारों में से एक में URI के लोगों को उनके डिफ़ॉल्ट स्थान, शरीर के बजाय पढ़ने के लिए बाध्य करने के लिए नहीं। पूर्णता के लिए [FromBody] विशेषता अनिवार्य रूप से जटिल प्रकारों के साथ विपरीत है। [१] docs.microsoft.com/en-us/aspnet/web-api/overview/… )
सीबी आंद्रोस १०'१

1

आप बस निम्नलिखित कर सकते हैं:

    [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


1

विधियाँ इस प्रकार होनी चाहिए:

[Route("api/[controller]")]
public class PersonsController : Controller
{
    [HttpGet("{id}")]
    public Person Get(int id)

    [HttpGet]
    public Person[] Get([FromQuery] string firstName, [FromQuery] string lastName, [FromQuery] string address)
}

ध्यान दें कि दूसरी विधि ऑब्जेक्ट्स की एक सरणी लौटाती है और कंट्रोलर नाम प्लूरर (पर्सन्स नॉट पर्सन) है।

इसलिए यदि आप आईडी द्वारा संसाधन प्राप्त करना चाहते हैं तो यह होगा:

api/persons/1

यदि आप कुछ खोज मापदंड जैसे पहले नाम और आदि से वस्तु लेना चाहते हैं, तो आप इस तरह से खोज कर सकते हैं:

api/persons?firstName=Name&...

और आगे बढ़ना अगर आप उस व्यक्ति के आदेश (उदाहरण के लिए) लेना चाहते हैं, तो यह इस तरह होना चाहिए:

api/persons/1/orders?skip=0&take=20

और एक ही नियंत्रक में विधि:

    [HttpGet("{personId}/orders")]
    public Orders[] Get(int personId, int skip, int take, etc..)

0
    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");
            }
        }
    }

0

सबसे सरल तरीका,

नियंत्रक:

[HttpGet("empId={empId}&startDate={startDate}&endDate={endDate}")]
 public IEnumerable<Validate> Get(int empId, string startDate, string endDate){}

डाकिया अनुरोध:

{router}/empId=1&startDate=2020-20-20&endDate=2020-20-20

सीखने का बिंदु: अनुरोध सटीक पैटर्न नियंत्रक द्वारा स्वीकार किया जाएगा।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.