【问题标题】:Azure mobile app and http get functionAzure 移动应用和 http 获取功能
【发布时间】:2018-04-17 05:26:59
【问题描述】:

非常抱歉,如果这是一个简单的问题。

我有 get 函数,它只是简单地返回一个字符串,但如果我想向 get 函数发送查询,我将如何在服务器端执行此操作?例如 &name=ABC 或 &Car=Ford?

namespace MobileAppAardra.Controllers
{
    [MobileAppController]
    public class HelloController : ApiController
    {
        // GET api/Hello
        public string Get( int a)
        {
            if (a==1)
            {
                return "Jambo"; // "Hello from custom controller!";
            }
            else
            {
                return "Hello World DJ"; // "Hello from custom controller!";
            }
            //return "Hello World DJ"; // "Hello from custom controller!";
        }
    }
}

谢谢你

【问题讨论】:

    标签: c# azure http controller azure-mobile-services


    【解决方案1】:

    我认为您可以使用简单的类作为模型。例如定义类

    public class User {
    
        public string Name { get; set; }
        public string Car { get; set; }
    }
    

    然后编辑您的 GET 方法以接受此类作为参数

    public string Get( User user) {
    
        if (ModelState.IsValid && user != null) {
    
            // Do whatever You want with class
    
            String name = user.Name;
            String Car = user.Car;
    
        }
    }
    

    如果您随后使用查询参数发出 GET 请求

    ?name=usersSuperName&car=bestCarEver
    

    这些参数将在上面的代码中可用。

    您也可以在客户端上使用相同的模型类,当您想要使用 MobileServiceClient.invokeApi(...) 其中一个方法覆盖接受模型时可用。

    【讨论】:

    • 如果解决了,请将其标记为关闭此主题的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多