从同一解决方案中的另一个项目中的视图调用类库中的函数

时间:2014-02-11 09:44:09

标签: c# jquery asp.net-mvc unit-testing qunit

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

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

  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项目的自定义视图中有一些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>

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

1 个答案:

答案 0 :(得分:0)

您需要注册一个正确的路线,将“QunitTest / someaction”等网址映射到您的控制器操作:

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

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