这两个方法可以在 js 里获取 action 的地址

纯 js

WebPathUtility.managementAction("actionName","controllerName",{Name:"test",Id:123})


WebPathUtility.managementAction("actionName","controllerName")



 

js + C#

WebPathUtility.map("~/xxx")



WebPathUtility.map('@(Url.ManagementAction("ActonName","ControllerName"))')

 

 

实现

WebPathUtility = {

webRoot: window.webRoot || "/",

map: function (path) {

if (!path) {

return path;

}

var wr = WebPathUtility.webRoot;

if (path.startsWith("~/")) {

return wr + path.substring(2);

} else if (path.startsWith("Attachments/")) {

return wr + path;

}

return path;

},

// WebPathUtility.managementAction("actionName","controllerName",{Name:"test",Id:123})

managementAction: function (actionName, controllerName, routeValues) {

var ps = routeValues ? WebPathUtility.convertJsonToQueryString(routeValues) : null;

return WebPathUtility.map("~/Management/" + controllerName + "/" + actionName) + ((ps ? ("?" + ps) : ps) || "");

},

convertJsonToQueryString: function (json, prefix) {

//convertJsonToQueryString({ Name: 1, Children: [{ Age: 1 }, { Age: 2, Hobby: "eat" }], Info: { Age: 1, Height: 80 } })

if (!json) return null;

var str = "";

for (var key in json) {

var val = json[key];

if (isJson(val)) {

str += WebPathUtility.convertJsonToQueryString(val, ((prefix || key) + "."));

} else if (typeof (val) == "object" && ("length" in val)) {

for (var i = 0; i < val.length; i++) {

//debugger

str += WebPathUtility.convertJsonToQueryString(val[i], ((prefix || key) + "[" + i + "]."));

}

}

else {

str += "&" + ((prefix || "") + key) + "=" + val;

}

}

return str ? str.substring(1) : str;

}

};

 

发表评论

您的电子邮箱地址不会被公开。