【问题标题】:Yet another ASP.NET MVC Route not found找不到另一个 ASP.NET MVC 路由
【发布时间】:2012-11-29 11:16:40
【问题描述】:

我正在尝试为 MVC 3 路由编写测试,但仍然没有得到匹配。为什么这在测试中不起作用?我要离开 http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx 并查看 MVC 代码,它可能与 VirtualPathProvider 有关?

全球.asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "import_DownloadTemplate",
        "{culture}/Client/{clientId}/DownloadTemplate",
        new { controller = "Import", action = "DownloadTemplate" },
        new { httpMethod = new HttpMethodConstraint("GET") });
}

进口控制器

[HttpGet]
[Cache(Order = 1)]
[OutputCache(Order = 2, Duration = 60, VaryByParam = "*")]
public ActionResult DownloadTemplate(string culture, long clientId)
{
    byte[] result = this.repository.GetTemplateByClientId(clientId, culture);

    return new FileContentResult(result, "application/vnd.ms-excel");
}

使用最小起订量进行测试

    [TestMethod]
    public void TestRoutes()
    {
        string url = "~/en-us/Client/1/DownloadTemplate";

        var httpContextMock = new Mock<HttpContextBase>();
        httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath)
            .Returns(url);

        RouteCollection routes = new RouteCollection();
        MvcApplication.RegisterRoutes(routes);

        var routeData = routes.GetRouteData(httpContextMock.Object);
        // routeData is null!

        Assert.AreEqual("import", routeData.Values["controller"].ToString());
        Assert.AreEqual("DownloadTemplate", routeData.Values["action"].ToString());
    }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    你必须设置 HttpMethod,所以在 TestRoutes 方法中添加这些行:

    string httpMethod = "GET";
    httpContextMock.Setup(c => c.Request.HttpMethod).Returns(httpMethod);
    

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 2011-11-15
      • 2016-05-22
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      相关资源
      最近更新 更多