在Asp.Net MVC控制器中读取Axios Get参数 [英] Reading Axios Get params in Asp.Net MVC controller

查看:703
本文介绍了在Asp.Net MVC控制器中读取Axios Get参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用axios get方法并将参数传递给asp.net mvc控制器.我可以读取单个值.但是我试图将所有值一起作为一个对象读取.我没有视图模型,并且试图将参数读取为通用对象.在c#控制器中用作参数的axios params数据类型是什么?我为buildurl和验证每个参数创建了一个单独的方法,但是是否可以选择一次验证所有参数?

I am using axios get method and passing params to asp.net mvc controller. I can read individual values. But I am trying to read all values together as one object. I do not have a view model and I am trying to read params as generic object. What will be axios params datatype to use in c# controller as parameter ? I created a seperate method for buildurl and validating each parameter but is there any option to validate all at once?

这有效

反应代码

export const GetRequestCall = () => {
  const getUrl = 'baseurl';

  return new Promise((resolve, reject) => {
    axios.get(getUrl, {
      params: {
        param1: 'abc',
        param2: 'efg'
      }
    })
      .then(response => {

      }).catch(error => reject(error));
  });
};

C#控制器代码

     //Read parameter as individual strings
        [HttpGet("[action]")]
        public async Task<string> GET(string param1, string param2)
        {
            try
            {  
                var url = BuildUri( param1, param2); 
             }
         }

这不起作用

控制器代码

 //Read parameters as a single object to do some logic. Tried 
    //[FromBody]object, Object, String as parameters datatypes for data
        [HttpGet("[action]")]
        public async Task<string> GET(Array data)
        {               
            try
            {
                var url = BuildUri( param1, param2); 
             }
         }

    private static string BuildUri(string BaseUrl, string param1, string param2)
    {
        var uriBuilder = new UriBuilder(BaseUrl);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        if (!string.IsNullOrEmpty(param1)) { query["param1"] = param1; }
        if (!string.IsNullOrEmpty(param2)) { query["param2"] = param2; }
        uriBuilder.Query = query.ToString();
        var url = uriBuilder.ToString();
        return url;
    }

我找到了在C#中使用名称值对构建查询字符串的选项,但是不确定如何将axios参数作为名称值对对象传递给c#控制器. 参考: https://codereview.stackexchange.com/questions/91783/constructing- a-query-string-using-stringbuilder

I found option to build query string with name value pairs in C# but not sure on how to pass axios params as name value pair object to c# controller. Ref: https://codereview.stackexchange.com/questions/91783/constructing-a-query-string-using-stringbuilder

推荐答案

可能有更好的方法,但是一种方法是使用像这样的 object [] 参数:

There are probably better ways to do it, but one way is to use an object[] parameter like this:

        [HttpGet("[action]")]
        public string GET(object[] objects)
        {
            string param1 = objects[0] as string;
            string param2 = objects[1] as string;
            try
            {
                var url = BuildUri(param1, param2);
            }
        }

此外,您不应该使用没有catch块的try块.希望对您有帮助

Also you should not use try blocks without catch blocks. I hope this helps

这篇关于在Asp.Net MVC控制器中读取Axios Get参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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