如何在web api中实现和调用get方法 [英] How to implemet and call get methods in web api

查看:98
本文介绍了如何在web api中实现和调用get方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public IEnumerable<string> Get(int id)
        {
            return new string[] { "sa1", "sa2" };
        }
        [HttpGet]
        public IEnumerable<string> password()
        {
            return new string[] { "password", "password" };
        }



但是当我尝试调用http:// localhost:49365 / api / mycontrollername / password时,它始终显示请求无效。

从不调用密码方法


But When i try to call http://localhost:49365/api/mycontrollername/password it's always showing The request is invalid.
It is never calling a password method

推荐答案

查看示例

我返回包含ErrorCode&的对象服务器端的JsonResult(即医师名单)

See the example
I return the object that contains ErrorCode & JsonResult (i.e List of Physician) from server side
public Result Get()
        {
            try
            {
                OResult.ErrorCode = 0;
                OResult.JsonResult= OPhysicianRepository.GetAllPhysician();                
                return OResult;
            }
            catch
            {
                OResult.ErrorCode = 1;
                OResult.JsonResult = null;              
                return OResult;
            }
        }





致电医师视图



To Call on Physician view


.getJSON(' / api / physician' function (contactsJsonPayload){
.getJSON('/api/physician', function (contactsJsonPayload) {


(contactsJsonPayload.JsonResult).each( function (i,item){
(contactsJsonPayload.JsonResult).each(function (i, item) {


这篇关于如何在web api中实现和调用get方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆