gpt4 book ai didi

c# - 从同一解决方案中另一个项目的 View 调用类库中的函数

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:26 26 4
gpt4 key购买 nike

我在类库中有这样一个函数

===============

  public class QUnitTestController : DesignerController
{
public ActionResult Index()
{
InitializeModel();

var designer = ((Designers)(Convert.ToInt32(DesignerId))).ToString();

ViewBag.ControllerName = designer;

return View("Index");
}

[HttpPost]
[ActionName("designdocument")]
public virtual ActionResult GetDesignDocument()
{
return GetJson("~/xmldata/Designer/QUnitDesignDoc.xml", true);

}

[HttpPost]
[ActionName("designconfig")]
public virtual ActionResult GetDesignConfig()
{

return GetJson("~/xmldata/Designer/QUnitDesignConfig.xml", true);
}

public ContentResult GetJson(string data, bool isUri)
{
var document = new XmlDocument();

if (isUri)
{
document.Load(Server.MapPath(data));
}
else
{
document.LoadXml(data);
}

string jsonText = JsonConvert.SerializeXmlNode(document.DocumentElement);
return Content(jsonText, "text/html", Encoding.UTF8);
}
}

我在另一个 MVC 项目的自定义 View 中有一些 jquery 代码,如下所示:-`

    <script type="text/javascript">

//var designerDocument = null;
//var designerConfig = null;
//var designerController = null;
//var zoom = null;


function Test() {
$.ajax({
url: "QUnitTest/designdocument",
type: "POST",
dataType: 'json',
async: true,//WHY??
data: param = "",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("success");
},
error: function () {
alert("something went wrong");
}
});

$.ajax({
url: "QUnitTest/designconfig",
type: "POST",
dataType: 'json',
async: true,//WHY??
data: param = "",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("success");
},
error: function () {
alert("something went wrong");
}
});

designerController = new Design.QUnitTestController();

zoom = 0;
}





module("Load Designer Module", {
setup: function () {
//Test();
}
});
test("Load Designer", function () {

ok(true, "body was clicked!");
var a = designerController.InitializeDesigner(designerDocument, designerConfig, "#designerContainer");
equal(a, "expected value");
});





</script>

问题是我无法调用类库中的函数。请告诉我我该怎么做才能让它发挥作用

最佳答案

您需要注册一个正确的路由来将“QUnitTest/someaction”之类的 url 映射到您的 Controller 操作:

检查您的 Global.asax.cs 或 RouteConfig 类并将以下代码添加到 RegisterRoutes 方法:

routes.MapRoute(
name: "QUnitTest",
url: "QUnitTest/{action}",
defaults: new { controller = "QUnitTest", action = "Index" },
namespaces: new[] { "NamespaceOfQUnitTestControllerClass" }
);

关于c# - 从同一解决方案中另一个项目的 View 调用类库中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21698196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com