如何在asp.net MVC中使用href从javascript打开新窗口中的打开窗口

时间:2017-09-21 15:48:40

标签: javascript ajax asp.net-mvc asp.net-web-api href

我有一个以下代码,我在asp.net MVC视图中打开一个外部URL链接。

现在我必须在打开链接时添加一个条目。 我的想法是我应该通过一些javascript函数来实现它,它将调用ajax调用我的web api控制器,然后打开一个新的url窗口

 @{
            var token = Session["SecurityToken"] as AppSecurityToken;
            if (token != null && token.Claims != null)
            {
                foreach (var claim in token.Claims)
                {
                    if (!String.IsNullOrWhiteSpace(claim.Description))
                    {
                        string imagepathv = Url.Content("~/") + "images/" + claim.Name + ".png";
                        string hreftext = $"{claim.Description}?sessionid={token.SessionId}";
                        <li>
                            <a href="" onclick="UserNameAccess('@hreftext');">
                                <img src="@imagepathv" />
                            </a>
                        </li>
                    }
                }
            }
        }


<script lang="javascript" type="text/javascript">
        function UserNameAccess(input) {
            window.open(input, "_blank");


            var baseSiteUrl = '@ConfigurationManager.AppSettings["WebApiBaseUrl"]';
            var request = { "email": "sadasd", "firstname": "asdasd", "lastname": "dasdasda" };

            $.ajax({
                type: "POST",
                url: baseSiteUrl + "DataAccessApi/LogUserClaimAccesshistory",
                data: JSON.stringify(request),
                datatype: "text",
                contentType: "application/json",
                success: function (resultData) {
                    var responseJson = resultData;
                    if (responseJson.Success === true) {
                        alert("success");
                    }
                    else {
                        alert("error1");
                    }
                },
                failure: function (error) {
                    alert(error);
                },
                error: function (jqXhr, textStatus) {

                    if (textStatus === 'timeout') {
                        alert("Request Timeout");
                        //do something. Try again perhaps?
                    } else {
                        alert(jqXhr.statusText);
                    }
                },
                timeout: 30000 // sets timeout to 3 seconds
            });
        }
    </script>       


 public class DataAccessController : ApiController
    {

        [Route("DataAccessApi/LogUserClaimAccesshistory")]
        public MyJsonResponse LogUserClaimAccesshistory(UserClaimHistory request)
        {
            MyJsonResponse result = new MyJsonResponse();
            return result;
        }
    }   


    public class MyJsonResponse
    {
        [JsonProperty]
        public bool Success { get; set; }

        [JsonProperty]
        public string Message { get; set; }

        [JsonProperty]
        public string CustomInfo { get; set; }
    }


    public class UserClaimHistory
    {
        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("firstname")]
        public string FirstName { get; set; }

        [JsonProperty("lastname")]
        public string LastName { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

为什么不使用以下代码?让@{ var token = Session["SecurityToken"] as AppSecurityToken; if(token != null && token.Claims != null) { foreach(var claim in token.Claims) { if(!String.IsNullOrWhiteSpace(claim.Description)) { string imagepathv = Url.Content("~/") + "images/" + claim.Name + ".png"; string hreftext = $"{claim.Description}?sessionid={token.SessionId}"; <li> <!-- We use the <a> tag to handle the new page opening --> <a href="@hreftext" onclick="UserNameAccess();" target="_blank"> <img src="@imagepathv"/> </a> </li> } } } } <!-- The javascript completly ignores the new page, and only focus on the API call --> <script lang="javascript" type="text/javascript"> function UserNameAccess() { var baseSiteUrl = '@ConfigurationManager.AppSettings["WebApiBaseUrl"]'; var request = { "email": "sadasd", "firstname": "asdasd", "lastname": "dasdasda" }; $.ajax({ type: "POST", url: baseSiteUrl + "DataAccessApi/LogUserClaimAccesshistory", data: JSON.stringify(request), datatype: "text", contentType: "application/json", success: function(resultData) { var responseJson = resultData; if (responseJson.Success === true) { alert("success"); } else { alert("error1"); } }, failure: function(error) { alert(error); }, error: function(jqXhr, textStatus) { if (textStatus === 'timeout') { alert("Request Timeout"); //do something. Try again perhaps? } else { alert(jqXhr.statusText); } }, timeout: 30000 // sets timeout to 3 seconds }); } </script> 标记打开网页,而JS负责API调用。

更新回答:

docker history