简体   繁体   English

C#Web Api(非MVC)字典参数

[英]C# Web Api (Non MVC) Dictionary parameters

I am trying to create a Web Api that will be used by many websites / apps. 我正在尝试创建一个将被许多网站/应用程序使用的Web Api。

The Api takes dictionary as parameter as it will be dynamic. Api将字典作为参数,因为它将是动态的。

C# Method: C#方法:

[HttpPost]
public Dictionary<string, string> ApiTest(Dictionary<string, string> parameters )
{
    Dictionary<string, string> dic_data = new Dictionary<string, string>();
    foreach (KeyValuePair<string, string> tmp in parameters)
        {
            dic_data[tmp.Key] = tmp.Value;
        }

    return dic_data;
}

Ajax Call to Method: Ajax调用方法:

Please note i left out contentType: "application/json" as it complains about it being including with CORS allowed 请注意我遗漏了contentType:“application / json”,因为它抱怨它允许包括CORS

var data = {"parameters":{"product_id":"1","product_model":"HFJ5G1.5","product_type":"plat","product_return":"graviteits"}};
//var data = {AlbumName: "Dirty Deeds",Songs:[ { SongName: "Problem Child"},{ SongName: "Squealer"}]};
var parameters = {};
parameters['1'] = 10;
parameters['2'] = 11;
$.ajax({
    type :'POST',
    url : 'http://localhost:8082/api/WebController/ApiTest/',
    dataType : 'json',
    data: parameters ,

    success : function(data) {
    console.log(data);

I have tried Binding the parameter as well: 我也尝试过绑定参数:

public class myParameters
{
    public Dictionary<string,string> parameters { get; set; }
}
public Dictionary<string, string> ApiTest(myParameters parameters )

Still no luck. 仍然没有运气。

On the browser console, I just keep receiving back "{}" from the API. 在浏览器控制台上,我只是继续从API接收“{}”。

I have looked at many examples but none seem to help. 我看了很多例子,但似乎没有任何帮助。 I'm still very new to C# so some of the comments and answers was just above me (like the ModelBinder stuff) 我对C#还是很陌生,所以一些评论和答案就在我的上方(比如ModelBinder的东西)

Passing List of KeyValuePair or IDictionary to Web Api Controller from Javascript 从Javascript将KeyValuePair或IDictionary的列表传递给Web Api控制器

Method with Dictionary Parameter in Asp.Net Web API Asp.Net Web API中带字典参数的方法

WebApi bind body to Json dictionary WebApi将主体绑定到Json字典

Deserialize JSON into dictionary in Web Api controller 在Web Api控制器中将JSON反序列化为字典

UPDATE : 更新

After a few more hours of searching, i decided to make a "local" call to the API from the same site that hosts the API. 经过几个小时的搜索,我决定从承载API的同一站点对API进行“本地”调用。 I was then able to put in place contentType: 'application/json; 然后我能够将contentType:'application / json; charset=utf-8', which had no problems with sending parsing JSON post object into Dictionary parameter :-( charset = utf-8',将解析JSON post对象发送到Dictionary参数没有问题:-(

So now my search continues of contentType + C# Api + Dictionary + CORS 所以现在我继续搜索contentType + C#Api + Dictionary + CORS

If it works fine from the same domain, you will need to enable CORS in that case, and your ajax call will include 如果它在同一个域中工作正常,则需要在这种情况下启用CORS,并且您的ajax调用将包括

crossDomain: true, dataType: "json", contentType: "application/json",

To enable CORS take a look at the example here 要启用CORS,请查看此处的示例

CORS was enabled on the server. 服务器上启用了CORS。 With simple binding parameters like strings it worked fine Crossdomain. 使用简单的绑定参数,如字符串,它工作得很好Crossdomain。 Dictionary where is failed. 字典哪里失败了。

However i had to go one step further on IIS to enable specifying the content type in the AJAX call 但是,我必须在IIS上更进一步,以便在AJAX调用中指定内容类型

jQuery CORS Content-type OPTIONS jQuery CORS内容类型选项

I have tested it without IIS content type header specified , and even if i add the crossDomain: true, I still get Jquery error complaining about CORS 我已经测试了它没有指定IIS内容类型标头,即使我添加了crossDomain:true,我仍然得到Jquery错误抱怨CORS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM