调用从我的控制器,而不是从我的asp.net MVC Web应用程序视图中的JSON API [英] Calling a JSON API from my controller instead of from the view in my asp.net mvc web application

查看:106
本文介绍了调用从我的控制器,而不是从我的asp.net MVC Web应用程序视图中的JSON API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个JSON API从我的asp.ner MVC Web应用程序BPM引擎。该API调用BPM构建如下: -

<$p$p><$c$c>http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=' +用户名

这里既有j_user&放大器;哈希paramertes重新presents并将其设置在BPM发动机侧的主登陆用户名和密码。
目前我使用的Java /脚本在视图级别从我的asp.net mvc的调用API: -

  $(文件)。就绪(函数(){
    VAR fullurl = 'http://localhost:8080/jw/web/json/workflow/package/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=' +用户名;
    $阿贾克斯({
        键入:GET,
        网址:fullurl,        数据类型:JSONP
        //的contentType:应用/ JSON的;字符集= UTF-8,
        成功:函数(结果){            $。每个(result.data,功能(键,VAL){                //格式化文本显示。
             //无功海峡= val.packageName +'| '+ val.packageId;
                无功海峡= val.packageName;
                //为产品添加列表项。
                $('&LT;李/&GT;',{文字:STR})
                .appendTo($('#产品'));            });
        }
    });
});

但我被告知,暴露主的登录用户名和密码都,也是LoginAS用户名从而重新presents登录用户的在asp.net mvc的是不安全的用户名,那我应该执行API调用服务器端而不是使API调用来自JavaScript代码。

但我的问题是我怎么能转换我上面code从MVC控制器端收到JSON,然后通过JSON的看法?
顺祝商祺


解决方案

您可以使用的 WebClient的火HTTP请求到指定的网址:

 公共类PackagesController:控制器
{
    公众的ActionResult列表()
    {
        使用(VAR的客户=新的WebClient())
        {
            VAR的查询= HttpUtility.ParseQueryString(的String.Empty);
            查询[为j_username] =克米特;
            查询[哈希] =9449B5ABCFA9AFDA36B801351ED3DF66;
            查询[loginAs] =some_username;
            VAR URL =新UriBuilder(HTTP://本地主机:8080 / JW / WEB / JSON /流程/包/表);
            url.Query = query.ToString();
            串JSON = client.DownloadString(url.ToString());
            返回内容(JSON,应用/ JSON);
        }
    }
}

或者你可以使用新的 HttpClient的在.NET 4.5介绍:

 公共类PackagesController:AsyncController
{
    公共异步任务&LT;&的ActionResult GT; ListPackages()
    {
        使用(VAR的客户=新的HttpClient())
        {
            VAR的查询= HttpUtility.ParseQueryString(的String.Empty);
            查询[为j_username] =克米特;
            查询[哈希] =9449B5ABCFA9AFDA36B801351ED3DF66;
            查询[loginAs] =some_username;
            VAR URL =新UriBuilder(HTTP://本地主机:8080 / JW / WEB / JSON /流程/包/表);
            url.Query = query.ToString();
            变种响应=等待client.GetAsync(url.ToString());
            VAR的结果=等待response.Content.ReadAsStringAsync();
            返回内容(由此,应用/ JSON);
        }
    }
}

和从你的JavaScript发送一个AJAX请求上述动作:

 &LT;脚本类型=文/ JavaScript的&GT;
    $(文件)。就绪(函数(){
        $阿贾克斯({
            网址:'@ Url.Action(列表,包),
            输入:GET,
            缓存:假的,
            成功:函数(结果){
                $。每个(result.data,功能(键,VAL){
                    无功海峡= val.packageName;
                    $('&LT;李/&GT;',{文字:STR})
                        .appendTo($('#产品'));
                });
            }
        });
    });
&LT; / SCRIPT&GT;

I need to call a JSON API for a BPM engine from my asp.ner mvc web application . The API call to the BPM is constructed as follow:-

http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=' + username

where both the j_user & hash paramertes represents a master login username and password which are set at the BPM engine side. Currently i am calling the API using java/script at the view level from my asp.net mvc:-

$(document).ready(function () {
    var fullurl = 'http://localhost:8080/jw/web/json/workflow/package/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=' + username ;
    $.ajax({
        type: "GET",
        url: fullurl, 

        dataType: "JSONP",
        // contentType: "application/json; charset=utf-8",
        success: function (result) {

            $.each(result.data, function (key, val) {

                // Format the text to display.
             //   var str = val.packageName + ' | ' + val.packageId;
                var str = val.packageName ;
                // Add a list item for the product.
                $('<li/>', { text: str })
                .appendTo($('#products'));

            });
        }
    });


});

But i was told that exposing both the master-login username and password and also the LoginAS username which represents the username of the login user at the asp.net mvc is not secure, AND THAT I SHOULD PERFORM THE API CALL AT THE SERVER SIDE INSTEAD OF MAKING THE API CALL FROM A JAVASCRIPT.

but my question is how i can convert my above code to receive the JSON from the mvc controller side and then pass the JSON to the view? Best Regards

解决方案

You could use a WebClient to fire an HTTP request to the specified url:

public class PackagesController: Controller
{
    public ActionResult List()
    {
        using (var client = new WebClient())
        {
            var query = HttpUtility.ParseQueryString(string.Empty);
            query["j_username"] = "kermit";
            query["hash"] = "9449B5ABCFA9AFDA36B801351ED3DF66";
            query["loginAs"] = "some_username";
            var url = new UriBuilder("http://localhost:8080/jw/web/json/workflow/package/list");
            url.Query = query.ToString();
            string json = client.DownloadString(url.ToString());
            return Content(json, "application/json");
        }
    }
}

or you could use the new HttpClient introduced in .NET 4.5:

public class PackagesController : AsyncController
{
    public async Task<ActionResult> ListPackages()
    {
        using (var client = new HttpClient())
        {
            var query = HttpUtility.ParseQueryString(string.Empty);
            query["j_username"] = "kermit";
            query["hash"] = "9449B5ABCFA9AFDA36B801351ED3DF66";
            query["loginAs"] = "some_username";
            var url = new UriBuilder("http://localhost:8080/jw/web/json/workflow/package/list");
            url.Query = query.ToString();
            var response = await client.GetAsync(url.ToString());
            var result = await response.Content.ReadAsStringAsync();
            return Content(result, "application/json");
        }
    }
}

and from your javascript send an AJAX request to the aforementioned action:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            url: '@Url.Action("List", "Packages")', 
            type: 'GET',
            cache: false,
            success: function (result) {
                $.each(result.data, function (key, val) {
                    var str = val.packageName;
                    $('<li/>', { text: str })
                        .appendTo($('#products'));
                });
            }
        });
    });
</script>

这篇关于调用从我的控制器,而不是从我的asp.net MVC Web应用程序视图中的JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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