小编kas*_*tan的帖子

在ASP.NET MVC中模拟Controller.Url.Action(字符串,字符串,对象,字符串)

我使用NUnitMoq库进行单元测试.我需要模拟重载的Url.Action(字符串,字符串,对象,字符串),因为我的控制器的动作使用它来获取Action的绝对URL.

我现在尝试(看看MockUrlAction测试):

[TestFixture]
public class ControllerTests   
{
    [Test]
    public void MockUrlAction()
    {
        var controller = new TestController();

        controller.Url = new UrlHelper(new RequestContext(MvcMoqHelpers.FakeHttpContext(), new RouteData()), GetRouteCollection());

        // it works
        Assert.AreEqual("/PathToAction", controller.Url.Action("TestAction")); 

        // but it doesn't work
        Assert.AreEqual("http://example.com/PathToAction", controller.Url.Action("TestAction", null, null, "http")); 
    }

    private RouteCollection GetRouteCollection()
    {
        BundleTable.MapPathMethod = MapBundleItemPath;
        var routes = new RouteCollection();
        RouteConfig.RegisterRoutes(routes);

        var adminArea = new AdminAreaRegistration();
        var adminAreaRegistrationContext = new AreaRegistrationContext(adminArea.AreaName, routes);
        adminArea.RegisterArea(adminAreaRegistrationContext);

        return routes;
    }
} …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc unit-testing moq urlhelper

10
推荐指数
1
解决办法
7301
查看次数

MySQL,按多个表中的多列排序

我有两个表:

Table of Artists (tbl_artist):

artist_id - primary key
artist_name - has index   


Table of Albums (tbl_album):

album_id - primary key
album_artist_id - foreign key, has index
album_name - has index too
Run Code Online (Sandbox Code Playgroud)

表在生产服务器上有很多记录(艺术家 - 60k,专辑 - 250k)。

并且在索引页面上有一个专辑列表,分页步长= 50。专辑按artist_name ASC、album_name ASC 排序。所以简化的查询如下:

SELECT *
FROM tbl_artist, tbl_album
WHERE album_artist_id = artist_id
ORDER BY artist_name, album_name
LIMIT 0, 50
Run Code Online (Sandbox Code Playgroud)

查询执行时间很长。可能是因为按不同表中的列排序。当我只留下 1 个排序时 - 查询立即执行。

在这种情况下可以做什么?非常感谢。

编辑:解释:

+----+-------------+---------------+--------+------------------+---------+---------+-----------------------------------+--------+---------------------------------+
| id | select_type | table         | type   | possible_keys    | key     | …
Run Code Online (Sandbox Code Playgroud)

mysql join sql-order-by

5
推荐指数
2
解决办法
6212
查看次数

在ASP.NET MVC中检查Ajax请求和返回Json结果的好方法

我想让我在控制器中的动作更加灵活.我的意思是共同的行动通常会返回:

...
return View("someView");
Run Code Online (Sandbox Code Playgroud)

或者,例如,如果Ajax:

...
return Json(new {result="ok"});
Run Code Online (Sandbox Code Playgroud)

我想要的是让我的行动更"多用".例如,我基于简单的非Ajax请求创建了我的UI层,然后我决定使它更加用户友好并添加了一些Ajax.这样我必须纠正一些动作以返回Json.

避免此类事情的最简单(也可能是最差)方法是在每个(或几乎每个)Action中编写以下代码:

if (Request.IsAjaxRequest) {
    return Json(new {result="ok"});
}
else { 
    return View("someView") 
}
Run Code Online (Sandbox Code Playgroud)

但当然这种方法完全与DRY的原则相冲突.

所以我想找到实现"多用途"的良好做法.

一种方法是编写一些这样的辅助方法:

public ActionResult CheckForAjax(ActionResult result)
{
  return ActionResult(result, Json(new {result="ok"}));
}

public ActionResult CheckForAjax(ActionResult result, Json json)
{
  if (Request.IsAjaxRequest) {
     return json;
  }
  else {
     return result;
  }
}
Run Code Online (Sandbox Code Playgroud)

这样我可以在Actions中调用助手:

return CheckForAjax(View(...));
Run Code Online (Sandbox Code Playgroud)

要么

return CheckForAjax(View(...), Json(new {myCustomJson="hi"});
Run Code Online (Sandbox Code Playgroud)

但我不知道这是好方法还是只是重新发明一些自行车:)也许最好使用动作过滤器?但我不知道如何将自定义Json传递给该过滤器......

谢谢你的任何建议

ajax asp.net-mvc

3
推荐指数
1
解决办法
1829
查看次数

标签 统计

asp.net-mvc ×2

ajax ×1

join ×1

moq ×1

mysql ×1

sql-order-by ×1

unit-testing ×1

urlhelper ×1