0

I am trying to pass a parameter from my API, but it does not seem to be taking it. I have a lastName (eventually will be replaced with a unique id) that I want to pass, which will ultimately route to a profile view or that person.

Here is my API Controller:

   public class GetInquiryController : BaseController
{
    private GetInquiry manager;
    public GetInquiryController(IConfiguration config) : base(config)
    {
        manager = new GetInquiry(config);
    }

    [HttpGet]
    [Route("api/[controller]")]
    public IEnumerable<InquiryModel> Get(string lastName)
    {
        return manager.GetInquiriesByUser(lastName);
    }
 }

And my logic for my stored procedure that gets the last name

  public class GetInquiry
{
    private readonly IConfiguration configuration;

    public GetInquiry(IConfiguration config)
    {
        configuration = config;
    }

    public IEnumerable<InquiryModel> GetInquiriesByUser(string lastName)
    {
        string ApplicationDB = configuration.GetConnectionString("ApplicationDB");
        List<InquiryModel> lstinquiry = new List<InquiryModel>();
        using (SqlConnection con = new SqlConnection(ApplicationDB))
        {
            SqlCommand cmd = new SqlCommand("spGetInquiriesByUser", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter lastNameParameter = cmd.Parameters.Add("lastName", SqlDbType.Char, 10);
            lastNameParameter.Value = lastName;
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                InquiryModel inquiry = new InquiryModel();
                //inquiry.FormID = rdr["FormID"].ToString();
                inquiry.firstName = rdr["firstName"].ToString();
                inquiry.lastName = rdr["lastName"].ToString();
                inquiry.SID = rdr["SID"].ToString();
                inquiry.email = rdr["email"].ToString();
                inquiry.phone = rdr["phone"].ToString();
                inquiry.email = rdr["email"].ToString();
                inquiry.employer = rdr["street"].ToString();
                inquiry.city = rdr["city"].ToString();
                inquiry.state = rdr["state"].ToString();
                inquiry.zip = rdr["zip"].ToString();
                inquiry.date = rdr["date"].ToString();
                inquiry.comment = rdr["comment"].ToString();

                lstinquiry.Add(inquiry);
            }
            con.Close();
        }
        return lstinquiry;
    }
  }
}

I am using VUE.js and calling my API

methods: {
  editItem(lastName) {
    let id = lastName
    this.$http.get('http://localhost:61601/api/GetInquiry/', {
      id: lastName
    }),

I have tried using my logic without IEnumerable, which i didn't think will make a difference in this case.

I guess the good thing is that I am getting,

"SqlException: Procedure or function 'spGetInquiriesByUser' expects parameter '@lastName', which was not supplied"

when type my API route into the browser, but when I add the lastName to the end of my route or Postman, I get a blank page.

Hope this makes sense, please let me know if i need to provide more detail and if someone can point me into the right direction!

3
  • How are you calling the API ? Commented Nov 14, 2018 at 18:55
  • @Shyju, i just updated my question. I am using VUE.js and calling my API in the method above. Commented Nov 14, 2018 at 18:58
  • Can you confirm that you are, in fact, passing lastName to the method? Perhaps, debugging with a breakpoint so you can verify that first and foremost. Commented Nov 14, 2018 at 19:00

1 Answer 1

1

You need to define from where your lastName parameter should be read.

If you want to read it from the route, use FromRoute attribute

[HttpGet("{lastName}")] //api/GetInquiry/yourlastname
public IEnumerable<InquiryModel> Get([FromRoute]string lastName)
{
    return manager.GetInquiriesByUser(lastName);
}

If you want to read it from the querystring, use FromQuery attribute

[HttpGet] //api/GetInquiry?lastName=yourlastname
public IEnumerable<InquiryModel> Get([FromQuery]string lastName)
{
    return manager.GetInquiriesByUser(lastName);
}

Note: Above solution works when moving the [Route("api/[controller]")] ontop of the GetInquiryController class.

Update When you added your vue.js api call it makes more sence why this is not working, you are actually calling the lastName parameter by the id name. Changing your id prop to lastName would work with above

methods: {
  editItem(lastName) {
    this.$http.get('http://localhost:61601/api/GetInquiry/', {
      lastName: lastName
    }),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.